Skip to content

Commit 340a305

Browse files
authored
feat(config): support custom AST transformers written in TypeScript (#3063)
Closes #2831
1 parent 3649e73 commit 340a305

40 files changed

+611
-132
lines changed

e2e/__tests__/ast-transformers.test.ts

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import path from 'path'
22

3+
import execa from 'execa'
4+
35
import { json as runWithJson } from '../run-jest'
46
import { runNpmInstall } from '../utils'
57

8+
const { createBundle } = require('../../scripts/lib/bundle')
9+
10+
const AST_TRANSFORMERS_DIR_NAME = 'ast-transformers'
11+
612
const executeTest = (testDir: string): void => {
713
test(`successfully runs the tests inside ${testDir} with isolatedModules: false`, () => {
814
const { json } = runWithJson(testDir)
@@ -18,15 +24,56 @@ const executeTest = (testDir: string): void => {
1824
}
1925

2026
describe('path-mapping', () => {
21-
executeTest('ast-transformers/path-mapping')
27+
executeTest(`${AST_TRANSFORMERS_DIR_NAME}/path-mapping`)
2228
})
2329

24-
const TRANSFORM_OPT_DIR_NAME = 'transformer-options'
25-
2630
describe('transformer-options', () => {
31+
const TRANSFORM_OPT_DIR_NAME = 'transformer-options'
32+
2733
beforeAll(() => {
28-
runNpmInstall(path.join(__dirname, '..', 'ast-transformers', TRANSFORM_OPT_DIR_NAME))
34+
runNpmInstall(path.join(__dirname, '..', AST_TRANSFORMERS_DIR_NAME, TRANSFORM_OPT_DIR_NAME))
35+
})
36+
37+
test(`successfully runs the tests inside ${AST_TRANSFORMERS_DIR_NAME}/${TRANSFORM_OPT_DIR_NAME}`, () => {
38+
const { json } = runWithJson(`${AST_TRANSFORMERS_DIR_NAME}/${TRANSFORM_OPT_DIR_NAME}`)
39+
40+
expect(json.success).toBe(true)
2941
})
42+
})
43+
44+
describe('hoist-jest', () => {
45+
const NON_TS_FACTORY_DIR_NAME = 'non-ts-factory'
46+
const TS_FACTORY_DIR_NAME = 'ts-factory'
47+
48+
describe('non-ts-factory', () => {
49+
const DIR = path.resolve(__dirname, '..', AST_TRANSFORMERS_DIR_NAME, 'hoist-jest', NON_TS_FACTORY_DIR_NAME)
3050

31-
executeTest(`ast-transformers/${TRANSFORM_OPT_DIR_NAME}`)
51+
beforeAll(() => {
52+
runNpmInstall(DIR)
53+
const bundle = createBundle()
54+
execa.sync('npm', ['install', '--no-package-lock', '--no-shrinkwrap', '--no-save', bundle], {
55+
cwd: DIR,
56+
})
57+
})
58+
59+
executeTest(`${AST_TRANSFORMERS_DIR_NAME}/hoist-jest/${NON_TS_FACTORY_DIR_NAME}`)
60+
})
61+
62+
describe('ts-factory', () => {
63+
beforeAll(() => {
64+
runNpmInstall(path.resolve(__dirname, '..', AST_TRANSFORMERS_DIR_NAME, 'hoist-jest', TS_FACTORY_DIR_NAME))
65+
})
66+
67+
executeTest(`${AST_TRANSFORMERS_DIR_NAME}/hoist-jest/${TS_FACTORY_DIR_NAME}`)
68+
})
69+
})
70+
71+
describe('transformer-in-ts', () => {
72+
const TRANSFORMER_IN_TS_DIR_NAME = `${AST_TRANSFORMERS_DIR_NAME}/transformer-in-ts`
73+
74+
test(`successfully runs the tests inside ${TRANSFORMER_IN_TS_DIR_NAME}`, () => {
75+
const { json } = runWithJson(TRANSFORMER_IN_TS_DIR_NAME)
76+
77+
expect(json.success).toBe(true)
78+
})
3279
})

e2e/__tests__/hoist-jest.test.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

e2e/hoist-jest/ts-factory/jest-isolated.config.js renamed to e2e/ast-transformers/hoist-jest/ts-factory/jest-isolated.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ module.exports = {
1414
react$: '<rootDir>/node_modules/react',
1515
},
1616
transform: {
17-
'^.+.[tj]sx?$': '<rootDir>/../../../dist/index.js',
17+
'^.+.[tj]sx?$': '<rootDir>/../../../../dist/index.js',
1818
},
1919
}

e2e/hoist-jest/ts-factory/package.json renamed to e2e/ast-transformers/hoist-jest/ts-factory/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"react$": "<rootDir>/node_modules/react"
1818
},
1919
"transform": {
20-
"^.+\\.[tj]sx?$": "<rootDir>/../../../dist/index.js"
20+
"^.+\\.[tj]sx?$": "<rootDir>/../../../../dist/index.js"
2121
}
2222
}
2323
}
File renamed without changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { color } from '../entry'
2+
3+
jest.mock('../entry', () => {
4+
return { color: 'blue' }
5+
})
6+
7+
test('should use custom AST transformer written in ts', () => {
8+
expect(color).toBe('blue')
9+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type Color = 'red' | 'blue'
2+
3+
export const color: Color = 'red'

e2e/ast-transformers/transformer-in-ts/package-lock.json

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"jest": {
3+
"globals": {
4+
"ts-jest": {
5+
"astTransformers": {
6+
"before": ["<rootDir>/../../../src/transformers/hoist-jest.ts"]
7+
}
8+
}
9+
},
10+
"transform": {
11+
"^.+\\.[tj]sx?$": "<rootDir>/../../../dist/index.js"
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)