This is a library that extend [ts-auto-mock](https://github.com/Typescript-TDD/ts-auto-mock) to be used with jasmine
npm install jest-ts-auto-mock
This is a library that extend ts-auto-mock to be used with jest
The intention of the library is to automatically assign jest mock to functions giving you type safety
typescript@^3.2.2
ts-jest@>=24 <27
- A Transformer needs to be provided at compile time.
We need to tell ts-jest to use ttypescript that allow us to use a transformer.
ts
{
"compilerOptions": {
...
"plugins": [
{ "transform": "ts-auto-mock/transformer", "cacheBetweenTests": false }
]
}
}
`
- Enable ttypescript into the ts-jest configuration
`ts
...
"globals": {
"ts-jest": {
"compiler": "ttypescript"
}
}
`- provide jest-ts-auto-mock config before your test
`ts
"jest": {
...
"setupFiles": [
"/config.ts"
]
...
},
`- config file
`ts
import 'jest-ts-auto-mock'`Examples
ts-jest-ttypesctiptUsage
1) create an interface
`ts
interface Interface {
methodToMock: () => string
}
`
2) create a mock
`ts
const mock: Interface = createMock();
`
3) get the method mock You can get the method spy in 2 different ways
Through method
`ts
import { On, method } from "ts-auto-mock/extension";
const mockMethod: Jest.Mock = On(mock).get(method(mock => mock.methodToMock));
`Through string
`ts
import { On, method } from "ts-auto-mock/extension";
const mockMethod: Jest.Mock = On(mock).get(method('methodToMock'));
`
4) trigger the method
`ts
someMethodThatWillTriggerInterfaceA();
expect(mockMethod).toHaveBeenCalled();
``* Vittorio Guerriero
* Giulio Caprino
This project is licensed under the MIT License