Skip to content

Commit

Permalink
feat: allow ast transformers to take in addl opts, fix kulshekhar#1942
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho committed Sep 12, 2020
1 parent bcb5a51 commit 8e2f3bf
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 83 deletions.
36 changes: 22 additions & 14 deletions docs/user/config/astTransformers.md
Expand Up @@ -7,8 +7,8 @@ TypeScript AST transformers and provide them to `ts-jest` to include into compil

The option is `astTransformers` and it allows ones to specify which 3 types of TypeScript AST transformers to use with `ts-jest`:

- `before` means your transformers get run before TS ones, which means your transformers will get raw TS syntax
instead of transpiled syntax (e.g `import` instead of `require` or `define` ).
- `before` means your transformers get run before TS ones, which means your transformers will get raw TS syntax
instead of transpiled syntax (e.g `import` instead of `require` or `define` ).
- `after` means your transformers get run after TS ones, which gets transpiled syntax.
- `afterDeclarations` means your transformers get run during `d.ts` generation phase, allowing you to transform output type declarations.

Expand All @@ -23,11 +23,17 @@ module.exports = {
globals: {
'ts-jest': {
astTransformers: {
before: ['my-custom-transformer'],
before: [
'my-custom-transformer',
{
path: 'my-custom-transformer-that-needs-extra-opts',
options: {},
},
],
},
}
}
};
},
},
}
```

</div><div class="col-md-6" markdown="block">
Expand All @@ -40,7 +46,10 @@ module.exports = {
"globals": {
"ts-jest": {
astTransformers: {
"before": ["my-custom-transformer"]
"before": ["my-custom-transformer", {
path: 'my-custom-transformer-that-needs-extra-opts',
options: {}
}]
}
}
}
Expand All @@ -55,9 +64,9 @@ module.exports = {
`ts-jest` is able to expose transformers for public usage to provide the possibility to opt-in/out for users. Currently
the exposed transformers are:

- `path-mapping` convert alias import/export to relative import/export path base on `paths` in `tsconfig`.
This transformer works similar to `moduleNameMapper` in `jest.config.js`. When using this transformer, one might not need
`moduleNameMapper` anymore.
- `path-mapping` convert alias import/export to relative import/export path base on `paths` in `tsconfig`.
This transformer works similar to `moduleNameMapper` in `jest.config.js`. When using this transformer, one might not need
`moduleNameMapper` anymore.

#### Example of opt-in transformers

Expand All @@ -72,9 +81,9 @@ module.exports = {
astTransformers: {
before: ['ts-jest/dist/transformers/path-mapping'],
},
}
}
};
},
},
}
```

</div><div class="col-md-6" markdown="block">
Expand All @@ -97,7 +106,6 @@ module.exports = {

</div></div>


### Writing custom TypeScript AST transformers

To write a custom TypeScript AST transformers, one can take a look at [the one](https://github.com/kulshekhar/ts-jest/tree/master/src/transformers) that `ts-jest` is using.
2 changes: 1 addition & 1 deletion e2e/__tests__/path-mapping.test.ts
Expand Up @@ -13,7 +13,7 @@ function executeTest(rootDirs?: string[]) {
},
astTransformers: {
before: [
'ts-jest/dist/transformers/path-mapping'
{path: 'ts-jest/dist/transformers/path-mapping'}
],
},
},
Expand Down
114 changes: 71 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8e2f3bf

Please sign in to comment.