Need help? Join us on Slack
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
registerMock will register your custom mock that will be used in favour of creating a new one
./person.ts
export interface Person {
id: string;
}
./person-fake.ts
import { Person } from './person';
export class PersonFake extends Person {
public id: string;
public name: string;
constructor() {
this.id = "Basic Id";
this.name = "Basic name";
}
}
./context.ts
import { registerMock } from 'ts-auto-mock';
import { Person } from './person';
import { PersonFake } from './person-fake';
registerMock<Person>(() => new PersonFake());
./my-test.ts
interface Wrapper {
person: Person;
}
const mock: Wrapper = createMock<Wrapper>();
mock.person // PersonFake
mock.person.id // "Basic Id"
mock.person.name // "Basic name"
When using a fake we recommend using the extension strategy to retrieve the fake object. An example of usage for Promise->FakePromise can be found in the test folder.
Note: You can use it only in the common file (webpack context.ts, mocha tsnode.js, etc), using registerMock
in other files will have unexpected results.
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 will convert to null when the type is not supported. Open this link to see what is not supported
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';
cacheBetweenTests: boolean;
}
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) |
||
cacheBetweenTests |
true |
When set to true it will reuse mocks between different tests |
When set to false it create new mocks for each different tests |
We currently support
- Logs for not supported types It will log any not supported type automatically converted to null. This is useful to report an issue or to investigate a potential bug
One of the main functionality of ts auto mock is to generate mocks and cache them.
Mocks are currently created in the test file making tests to depend to each other
Example:
- test1.test.ts has a createMock of Interface.
- test2.test.ts has a createMock of Interface.
- test1.test.ts will have the registration of Interface mock
- test2.test.ts will have a registration import.
If test2 run in a different context than test1 it will not be able to access to the same mock.
Set this property to false when your test run in different context.
We are working on an issue to make sure tests do not depend to each other but they will still take advance of a cache system
This project is licensed under the MIT License