Skip to content

Commit acb2303

Browse files
longlhoahnpnl
authored andcommitted
fix: add unit test and e2e test
1 parent 798beaa commit acb2303

9 files changed

Lines changed: 178 additions & 17 deletions

File tree

docs/user/config/astTransformers.md

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,46 @@ The option is `astTransformers` and it allows ones to specify which 3 types of T
1414

1515
### Examples
1616

17+
#### Basic Transformers
18+
19+
<div class="row"><div class="col-md-6" markdown="block">
20+
21+
```js
22+
// jest.config.js
23+
module.exports = {
24+
// [...]
25+
globals: {
26+
'ts-jest': {
27+
astTransformers: {
28+
before: ['my-custom-transformer'],
29+
},
30+
},
31+
},
32+
}
33+
```
34+
35+
</div><div class="col-md-6" markdown="block">
36+
37+
```js
38+
// OR package.json
39+
{
40+
// [...]
41+
"jest": {
42+
"globals": {
43+
"ts-jest": {
44+
astTransformers: {
45+
"before": ["my-custom-transformer"]
46+
}
47+
}
48+
}
49+
}
50+
}
51+
```
52+
53+
</div></div>
54+
55+
#### Configuring transformers with options
56+
1757
<div class="row"><div class="col-md-6" markdown="block">
1858

1959
```js
@@ -24,10 +64,9 @@ module.exports = {
2464
'ts-jest': {
2565
astTransformers: {
2666
before: [
27-
'my-custom-transformer',
2867
{
2968
path: 'my-custom-transformer-that-needs-extra-opts',
30-
options: {},
69+
options: {}, // extra options to pass to transformers here
3170
},
3271
],
3372
},
@@ -46,9 +85,9 @@ module.exports = {
4685
"globals": {
4786
"ts-jest": {
4887
astTransformers: {
49-
"before": ["my-custom-transformer", {
88+
"before": [{
5089
path: 'my-custom-transformer-that-needs-extra-opts',
51-
options: {}
90+
options: {} // extra options to pass to transformers here
5291
}]
5392
}
5493
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { LogContexts, LogLevels } = require('bs-logger')
2+
3+
function factory(cs, extraOpts = Object.create(null)) {
4+
const logger = cs.logger.child({ namespace: 'dummy-transformer' })
5+
const ts = cs.compilerModule
6+
logger.debug('Dummy transformer with extra options', JSON.stringify(extraOpts))
7+
8+
function createVisitor(_ctx, _sf) {
9+
return (node) => node
10+
}
11+
12+
return (ctx) =>
13+
logger.wrap({ [LogContexts.logLevel]: LogLevels.debug, call: null }, 'visitSourceFileNode(): dummy', (sf) =>
14+
ts.visitNode(sf, createVisitor(ctx, sf))
15+
)
16+
}
17+
18+
exports.factory = factory
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const a = 1;
2+
3+
it('should pass', () => {
4+
expect(a).toEqual(1);
5+
})
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`AST transformers with extra options should pass using template "default" 1`] = `
4+
Array [
5+
"[level:20] Dummy transformer with extra options {\\"foo\\":\\"bar\\"}",
6+
]
7+
`;
8+
9+
exports[`AST transformers with extra options should pass using template "with-babel-7" 1`] = `
10+
Array [
11+
"[level:20] Dummy transformer with extra options {\\"foo\\":\\"bar\\"}",
12+
]
13+
`;
14+
15+
exports[`AST transformers with extra options should pass using template "with-babel-7-string-config" 1`] = `
16+
Array [
17+
"[level:20] Dummy transformer with extra options {\\"foo\\":\\"bar\\"}",
18+
]
19+
`;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { configureTestCase } from '../__helpers__/test-case'
2+
import { allValidPackageSets } from '../__helpers__/templates'
3+
import { existsSync } from "fs"
4+
import { LogContexts, LogLevels } from 'bs-logger'
5+
6+
describe('AST transformers', () => {
7+
describe('with extra options', () => {
8+
const testCase = configureTestCase('ast-transformers/with-extra-options', {
9+
env: { TS_JEST_LOG: 'ts-jest.log' },
10+
tsJestConfig: {
11+
astTransformers: {
12+
before: [{
13+
path: require.resolve('../__cases__/ast-transformers/with-extra-options/foo'),
14+
options: {
15+
foo: 'bar',
16+
},
17+
}],
18+
},
19+
},
20+
})
21+
22+
testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => {
23+
it(testLabel, () => {
24+
const result = runTest()
25+
expect(result.status).toBe(0)
26+
expect(existsSync(result.logFilePath)).toBe(true)
27+
const filteredEntries = result.logFileEntries
28+
// keep only debug and above
29+
.filter(m => (m.context[LogContexts.logLevel] || 0) >= LogLevels.debug)
30+
// simplify entries
31+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
32+
.map(e => result.normalize(`[level:${e.context[LogContexts.logLevel]}] ${e.message}`))
33+
.filter(logging => logging.includes('Dummy transformer with extra options'))
34+
expect(filteredEntries).toMatchSnapshot()
35+
})
36+
})
37+
})
38+
})

e2e/__tests__/path-mapping.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function executeTest(rootDirs?: string[]) {
1313
},
1414
astTransformers: {
1515
before: [
16-
{path: 'ts-jest/dist/transformers/path-mapping'}
16+
'ts-jest/dist/transformers/path-mapping'
1717
],
1818
},
1919
},

src/config/__snapshots__/config-set.spec.ts.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,14 @@ exports[`tsJest transformers should display deprecation warning message when con
355355
"[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
356356
"
357357
`;
358+
359+
exports[`tsJest transformers should support transformers with options 1`] = `
360+
Array [
361+
Object {
362+
"options": Object {
363+
"foo": 1,
364+
},
365+
"path": Any<String>,
366+
},
367+
]
368+
`;

src/config/config-set.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,37 @@ describe('tsJest', () => {
161161
expect(logger.target.lines[1]).toMatchSnapshot()
162162
})
163163

164+
it('should support transformers with options', () => {
165+
const cs = createConfigSet({
166+
jestConfig: {
167+
rootDir: 'src',
168+
cwd: 'src',
169+
globals: {
170+
'ts-jest': {
171+
astTransformers: {
172+
before: [
173+
{
174+
path: 'dummy-transformer',
175+
options: {
176+
foo: 1,
177+
},
178+
},
179+
],
180+
},
181+
},
182+
},
183+
} as any,
184+
logger,
185+
resolve: null,
186+
})
187+
188+
expect(cs.tsJest.transformers.before).toMatchSnapshot([
189+
{
190+
path: expect.any(String),
191+
},
192+
])
193+
})
194+
164195
it.each([
165196
{},
166197
{

src/config/config-set.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import { TSError } from '../utils/ts-error'
5757
const logger = rootLogger.child({ namespace: 'config' })
5858

5959
interface AstTransformerObj<T = Record<string, unknown>> {
60-
module: AstTransformerDesc
60+
transformModule: AstTransformerDesc
6161
options?: T
6262
}
6363

@@ -432,7 +432,7 @@ export class ConfigSet {
432432
let astTransformers: AstTransformer = {
433433
before: [
434434
...internalAstTransformers.map((transformer) => ({
435-
module: transformer,
435+
transformModule: transformer,
436436
})),
437437
],
438438
}
@@ -444,10 +444,10 @@ export class ConfigSet {
444444
...transformers.before.map((transformer) =>
445445
typeof transformer === 'string'
446446
? {
447-
module: require(transformer),
447+
transformModule: require(transformer),
448448
}
449449
: {
450-
module: require(transformer.path),
450+
transformModule: require(transformer.path),
451451
options: transformer.options,
452452
},
453453
),
@@ -460,10 +460,10 @@ export class ConfigSet {
460460
after: transformers.after.map((transformer) =>
461461
typeof transformer === 'string'
462462
? {
463-
module: require(transformer),
463+
transformModule: require(transformer),
464464
}
465465
: {
466-
module: require(transformer.path),
466+
transformModule: require(transformer.path),
467467
options: transformer.options,
468468
},
469469
),
@@ -475,10 +475,10 @@ export class ConfigSet {
475475
afterDeclarations: transformers.afterDeclarations.map((transformer) =>
476476
typeof transformer === 'string'
477477
? {
478-
module: require(transformer),
478+
transformModule: require(transformer),
479479
}
480480
: {
481-
module: require(transformer.path),
481+
transformModule: require(transformer.path),
482482
options: transformer.options,
483483
},
484484
),
@@ -494,14 +494,14 @@ export class ConfigSet {
494494
@Memoize()
495495
get tsCustomTransformers(): CustomTransformers {
496496
let customTransformers: CustomTransformers = {
497-
before: this.astTransformers.before.map((t) => t.module.factory(this, t.options)) as TransformerFactory<
497+
before: this.astTransformers.before.map((t) => t.transformModule.factory(this, t.options)) as TransformerFactory<
498498
SourceFile
499499
>[],
500500
}
501501
if (this.astTransformers.after) {
502502
customTransformers = {
503503
...customTransformers,
504-
after: this.astTransformers.after.map((t) => t.module.factory(this, t.options)) as TransformerFactory<
504+
after: this.astTransformers.after.map((t) => t.transformModule.factory(this, t.options)) as TransformerFactory<
505505
SourceFile
506506
>[],
507507
}
@@ -510,7 +510,7 @@ export class ConfigSet {
510510
customTransformers = {
511511
...customTransformers,
512512
afterDeclarations: this.astTransformers.afterDeclarations.map((t) =>
513-
t.module.factory(this, t.options),
513+
t.transformModule.factory(this, t.options),
514514
) as TransformerFactory<Bundle | SourceFile>[],
515515
}
516516
}
@@ -721,7 +721,7 @@ export class ConfigSet {
721721
digest: this.tsJestDigest,
722722
transformers: Object.values(this.astTransformers)
723723
.reduce((acc, val) => acc.concat(val), [])
724-
.map((t: AstTransformerDesc) => `${t.name}@${t.version}`),
724+
.map(({ transformModule }: AstTransformerObj) => `${transformModule.name}@${transformModule.version}`),
725725
jest,
726726
tsJest: this.tsJest,
727727
babel: this.babel,

0 commit comments

Comments
 (0)