A Typescript transformer that will allow you to create mock for any types (Interfaces, Classes, ...) without need to create manual fakes/mocks.
Let's have a look.
- If you are interested to use it with jasmine please go to jasmine-ts-auto-mock
- If you are interested to use it with jest please go to jest-ts-auto-mock
typescript@^3.2.2
A Transformer needs to be provided at compile time. There are different ways to do it. Please read the following guide to find your configuration
import { createMock } from 'ts-auto-mock';
interface Person {
id: string;
getName(): string;
details: {
phone: number
}
}
const mock = createMock<Person>();
mock.id // ""
mock.getName() // ""
mock.details // "{phone: 0} "
You can also define default values to overrides specific fields You dont have to provide the entire interface, just a partial of the one to mock
import { createMock } from 'ts-auto-mock';
interface Person {
id: string;
getName(): string;
details: {
phone: number
}
}
const mock = createMock<Person>({
details: {
phone: 07423232323
}
});
mock.id // ""
mock.getName() // ""
mock.details // "{phone: 07423232323} "
createMock list it will create a list of mocks automatically
import { createMockList } from 'ts-auto-mock';
interface Person {
id: string;
}
const mockList = createMockList<Person>(2);
mockList.length // 2
You can define a function to overrides specific fields The function will have access to the current index
import { createMockList } from 'ts-auto-mock';
interface Person {
id: string;
}
const mockList = createMockList<Person>(2, (index: number) => {
return {
id: "id" + index
}
});
mockList[0].id // id0
mockList[1].id // id1
The library try to convert the type given to createMock so you dont need to create concrete mock manually. Open this link to see more examples
The library allows you to extends some functionality to work nicely with framework like jasmine or jest Open this link to see more examples
tsAutoMockTransformer(program: ts.Program, options: TsAutoMockOptions)
interface TsAutoMockOptions {
debug: boolean | 'file' | 'console'
}
options:
Name | Default | Description |
---|---|---|
debug |
false |
When set to true or console it will log to the console |
When set to file it will log to a file (tsAutoMock.log) |
This project is licensed under the MIT License