Installation
To make this library work you need to apply the ts-auto-mock
transformer at compile time.
Unfortunately, TypeScript itself does not currently provide any easy way to use custom transformers out of the box.
There are different ways to do this depending on how you are currently compiling your TypeScript files
jest
+ ts-jest
+ ttypescript
Steps
- Install the dependencies
npm install jest ts-jest typescript ttypescript jest-ts-auto-mock ts-auto-mock
you need to have ttypescript
installed to patch and enable the TypeScript-runtime to apply the transformer.
- Add the transformer to your
tsconfig.json
..."compilerOptions": {..."plugins": [{"transform": "ts-auto-mock/transformer","cacheBetweenTests": false}]...}...
Remember to set
cacheBetweenTests
tofalse
because Jest runs tests in parallel and ts-auto-mock doesn't yet support caching across parallel testsYou can find a JSON example here
- Add
ts-jest
to the transformation pattern in the Jest config
package.json
/ jest.config.js
(without the jest
scope)
..."jest": {"transform": {".(ts|tsx)": "ts-jest"}}...
- You can find a JSON example here
- Add
ttypescript
to be used asts-jest
compiler
package.json
/ jest.config.js
(without the jest
scope)
..."jest": {"transform": {"^.+\\.tsx?$": ["ts-jest",{"compiler": "ttypescript"}]}}...
- You can find a JSON example here
- Add
jest-ts-auto-mock
config file as your setup file
package.json
/ jest.config.js
(without the jest
scope)
..."jest": {"setupFiles": ["<rootDir>config.ts"]}...
config.ts
import 'jest-ts-auto-mock';
- Create your first test
import { createMock } from 'ts-auto-mock';interface Interface {a: string;b: number;}describe('reuse', () => {let mock: Interface;beforeEach(() => {mock = createMock<Interface>();});it('should work', () => {expect(mock.a).toBe('');});});
- Run your test
package.json
..."scripts": {..."test": "jest"...}...
npm run test
... and you are all done!
Config options
In the tsconfig.json you can add the config options next to the transformer name:
"plugins": [{"transform": "ts-auto-mock/transformer","cacheBetweenTests": false,"features": ["random"]}]
jest
+ ts-jest
+ ts-patch
Steps
- Install the dependencies
npm install jest ts-jest typescript ts-patch jest-ts-auto-mock ts-auto-mock
you need to have ts-patch
installed to patch and enable the TypeScript-runtime to apply the transformer.
- Add the transformer to your
tsconfig.json
..."compilerOptions": {..."plugins": [{"transform": "ts-auto-mock/transformer","cacheBetweenTests": false}]...}...
Remember to set
cacheBetweenTests
tofalse
because Jest runs tests in parallel and ts-auto-mock doesn't yet support caching across parallel testsYou can find a JSON example here
- Add
ts-jest
to the transformation pattern in the Jest config
package.json
/ jest.config.js
(without the jest
scope)
..."jest": {"transform": {".(ts|tsx)": "ts-jest"}}...
- You can find a JSON example here
- Add
ts-patch install
to your prepare script in the package json
package.json
..."scripts": {...,"prepare": "ts-patch install -s",...}...
- You can find a JSON example here
- Add
jest-ts-auto-mock
config file as your setup file
package.json
/ jest.config.js
(without the jest
scope)
..."jest": {"setupFiles": ["<rootDir>config.ts"]}...
config.ts
import 'jest-ts-auto-mock';
- Create your first test
import { createMock } from 'ts-auto-mock';interface Interface {a: string;b: number;}describe('reuse', () => {let mock: Interface;beforeEach(() => {mock = createMock<Interface>();});it('should work', () => {expect(mock.a).toBe('');});});
- Run your test
package.json
..."scripts": {..."test": "jest"...}...
npm run test
... and you are all done!
Config options
In the tsconfig.json you can add the config options next to the transformer name:
"plugins": [{"transform": "ts-auto-mock/transformer","cacheBetweenTests": false,"features": ["random"]}]
Webpack
With Webpack, You can use any TypeScript-related loader that supports custom transformers, e.g. awesome-typescript-loader
or ts-loader
:
Steps
webpack.config.js
const tsAutoMockTransformer = require('ts-auto-mock/transformer').default;module.exports = {// ...module: {rules: [{test: /\.ts$/,loader: 'ts-loader', // or 'awesome-typescript-loader'options: {getCustomTransformers: program => ({before: [tsAutoMockTransformer(program)]})}}]}};
Config options
In webpack.config.js you can pass a second parameter to the transformer:
before: [tsAutoMockTransformer(program, {features: ['random']})]
ttypescript
See ttypescript's README for more information
Steps
tsconfig.json
{"compilerOptions": {//...,"plugins": [{ "transform": "ts-auto-mock/transformer" }]},//...}
Config options
In the tsconfig.json you can add the config options next to the transformer name:
"plugins": [{"transform": "ts-auto-mock/transformer","cacheBetweenTests": false,"features": ["random"]}]
ts-patch
Steps
Command to run to install it:
npm i ts-patchts-patch install
tsconfig.json
{"compilerOptions": {//...,"plugins": [{ "transform": "ts-auto-mock/transformer" }]},//...}
Config options
In the tsconfig.json you can add the config options next to the transformer name:
"plugins": [{"transform": "ts-auto-mock/transformer","cacheBetweenTests": false,"features": ["random"]}]
ts-node + Mocha
Steps
tsnode.js
const tsAutoMockTransformer = require('ts-auto-mock/transformer').default;require('ts-node').register({transformers: program => ({before: [tsAutoMockTransformer(program)]})});
Then inject the registration with the --require
flag that Mocha passes on to Node.js:
mocha --require './tsnode.js' --watch-extensions ts,tsx "test/**/*.{ts,tsx}"
Config options
In tsnode.js you can pass a second parameter to the transformer:
before: [tsAutoMockTransformer(program, {features: ['random']})]