Unfortunately, TypeScript itself does not currently provide any easy way to use custom transformers (See microsoft/TypeScript#14419). The followings are the example usage of the custom transformer.
// 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)
]
})
}
}
]
}
};
See ttypescript's README for how to use this with module bundlers such as webpack or Rollup.
tsconfig.json
{
"compilerOptions": {
...,
"plugins": [
{ "transform": "ts-auto-mock/transformer" }
]
},
...
}
npm i -g ts-patch
ts-patch install
tsconfig.json
{
"compilerOptions": {
...,
"plugins": [
{ "transform": "ts-auto-mock/transformer" }
]
},
...
}
tsnode.js
const tsAutoMockTransformer = require('ts-auto-mock/transformer').default;
require("ts-node").register({
transformers: program => ({
before: [
tsAutoMockTransformer(program)
]
})
});
Command to run
mocha --require './tsnode.js' --watch-extensions ts,tsx "test/**/*.{ts,tsx}"
tsnode.js
require("ts-node").register({ compiler: 'ttypescript' });
tsconfig.json
{
"compilerOptions": {
...,
"plugins": [
{ "transform": "ts-auto-mock/transformer" }
]
},
...
}
Command to run
mocha --require './tsnode.js' --watch-extensions ts,tsx "test/**/*.{ts,tsx}"