Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 58 additions & 11 deletions docs/user/config/astTransformers.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ 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.

### Examples

#### Basic Transformers

<div class="row"><div class="col-md-6" markdown="block">

```js
Expand All @@ -25,9 +27,52 @@ module.exports = {
astTransformers: {
before: ['my-custom-transformer'],
Comment thread
longlho marked this conversation as resolved.
},
},
},
}
```

</div><div class="col-md-6" markdown="block">

```js
// OR package.json
{
// [...]
"jest": {
"globals": {
"ts-jest": {
astTransformers: {
"before": ["my-custom-transformer"]
}
}
}
}
};
}
```

</div></div>

#### Configuring transformers with options

<div class="row"><div class="col-md-6" markdown="block">

```js
// jest.config.js
module.exports = {
// [...]
globals: {
'ts-jest': {
astTransformers: {
before: [
{
path: 'my-custom-transformer-that-needs-extra-opts',
options: {},
},
],
},
},
},
}
```

</div><div class="col-md-6" markdown="block">
Expand All @@ -40,7 +85,10 @@ module.exports = {
"globals": {
"ts-jest": {
astTransformers: {
"before": ["my-custom-transformer"]
"before": [{
path: 'my-custom-transformer-that-needs-extra-opts',
options: {}
}]
}
}
}
Expand All @@ -55,9 +103,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 +120,9 @@ module.exports = {
astTransformers: {
before: ['ts-jest/dist/transformers/path-mapping'],
},
}
}
};
},
},
}
```

</div><div class="col-md-6" markdown="block">
Expand All @@ -97,7 +145,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.
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.

11 changes: 11 additions & 0 deletions src/config/__snapshots__/config-set.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,14 @@ exports[`tsJest transformers should display deprecation warning message when con
"[level:40] The configuration for astTransformers as string[] is deprecated and will be removed in ts-jest 27. Please define your custom AST transformers in a form of an object. More information you can check online documentation https://kulshekhar.github.io/ts-jest/user/config/astTransformers
"
`;

exports[`tsJest transformers should support transformers with options 1`] = `
Array [
Object {
"options": Object {
"foo": 1,
},
"path": Any<String>,
},
]
`;
31 changes: 31 additions & 0 deletions src/config/config-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,37 @@ describe('tsJest', () => {
expect(logger.target.lines[1]).toMatchSnapshot()
})

it('should support transformers with options', () => {
const cs = createConfigSet({
jestConfig: {
rootDir: 'src',
cwd: 'src',
globals: {
'ts-jest': {
astTransformers: {
before: [
{
path: 'dummy-transformer',
options: {
foo: 1,
},
},
],
},
},
},
} as any,
logger,
resolve: null,
})

expect(cs.tsJest.transformers.before).toMatchSnapshot([
{
path: expect.any(String),
},
])
})

it.each([
{},
{
Expand Down
Loading