Skip to content

Commit 3325186

Browse files
authored
feat(transformers): add path-mapping custom AST transformer (#1927)
Usage detail see https://kulshekhar.github.io/ts-jest/user/config/astTransformers
1 parent cd60dc7 commit 3325186

33 files changed

Lines changed: 4602 additions & 29 deletions

.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ module.exports = {
4141
'getter-return': 'warn',
4242
'guard-for-in': 'error',
4343
'id-match': 'error',
44+
'jest/valid-title': 'off',
45+
'jest/no-conditional-expect': 'off',
4446
'jsdoc/check-alignment': 'error',
4547
'jsdoc/check-indentation': 'error',
4648
'jsdoc/newline-after-description': 'warn',

docs/user/config/astTransformers.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,54 @@ module.exports = {
5050

5151
</div></div>
5252

53+
### Public transformers
54+
55+
`ts-jest` is able to expose transformers for public usage to provide the possibility to opt-in/out for users. Currently
56+
the exposed transformers are:
57+
58+
- `path-mapping` convert alias import/export to relative import/export path base on `paths` in `tsconfig`.
59+
This transformer works similar to `moduleNameMapper` in `jest.config.js`. When using this transformer, one might not need
60+
`moduleNameMapper` anymore.
61+
62+
#### Example of opt-in transformers
63+
64+
<div class="row"><div class="col-md-6" markdown="block">
65+
66+
```js
67+
// jest.config.js
68+
module.exports = {
69+
// [...]
70+
globals: {
71+
'ts-jest': {
72+
astTransformers: {
73+
before: ['ts-jest/dist/transformers/path-mapping'],
74+
},
75+
}
76+
}
77+
};
78+
```
79+
80+
</div><div class="col-md-6" markdown="block">
81+
82+
```js
83+
// OR package.json
84+
{
85+
// [...]
86+
"jest": {
87+
"globals": {
88+
"ts-jest": {
89+
astTransformers: {
90+
"before": ["ts-jest/dist/transformers/path-mapping"]
91+
}
92+
}
93+
}
94+
}
95+
}
96+
```
97+
98+
</div></div>
99+
100+
53101
### Writing custom TypeScript AST transformers
54102

55103
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.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { getWelcomeMessage } from '@share/foo'
2+
3+
test('should return welcome message', () => {
4+
const userName = 'github-user'
5+
6+
expect(getWelcomeMessage(userName)).toEqual(`yolo ${userName}`)
7+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
test('should return welcome message', async () => {
2+
const userName = 'github-user'
3+
const foo = await import('@share/foo')
4+
5+
expect(foo.getWelcomeMessage(userName)).toEqual(`yolo ${userName}`)
6+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { getWelcomeMessage } from '@share/export'
2+
3+
test('should return welcome message', async () => {
4+
const userName = ''
5+
6+
expect(getWelcomeMessage(userName)).toEqual(`yolo ${userName}`)
7+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import foo from '@share/foo'
2+
3+
test('should return welcome message', () => {
4+
const userName = 'github-user'
5+
6+
expect(foo(userName)).toBe(`yolo ${userName}`)
7+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import foo = require('@share/foo')
2+
3+
test('should return welcome message', () => {
4+
const userName = 'github-user'
5+
6+
expect(foo.getWelcomeMessage(userName)).toEqual(`yolo ${userName}`)
7+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const foo = require('@share/foo')
2+
3+
test('should return welcome message', () => {
4+
const userName = 'github-user'
5+
6+
expect(foo.getWelcomeMessage(userName)).toEqual(`yolo ${userName}`)
7+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as foo from '@share/foo'
2+
3+
test('should return welcome message', () => {
4+
const userName = 'github-user'
5+
6+
expect(foo.getWelcomeMessage(userName)).toEqual(`yolo ${userName}`)
7+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Foo } from '@share/foo'
2+
3+
test('should work', () => {
4+
const a: Foo = {
5+
bar: 1,
6+
}
7+
8+
expect(a).toBeTruthy()
9+
})

0 commit comments

Comments
 (0)