Skip to content

Latest commit

 

History

History
113 lines (94 loc) · 2.04 KB

TRANSFORMER.md

File metadata and controls

113 lines (94 loc) · 2.04 KB

Transformer

How to use the custom transformer

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 (with ts-loader or awesome-typescript-loader)

// 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)
              ]
          })
        }
      }
    ]
  }
};

ttypescript

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" }
    ]
  },
  ...
}

ts-patch

See ts-patch's README

npm i -g ts-patch
ts-patch install

tsconfig.json

{
  "compilerOptions": {
    ...,
    "plugins": [
      { "transform": "ts-auto-mock/transformer" }
    ]
  },
  ...
}

ts-node

mocha

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}"

mocha + ttypescript

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}"