Skip to content

Commit a72f51d

Browse files
authored
refactor(transformers): use ts factory API for hoisting (#3058)
1 parent 7b602df commit a72f51d

17 files changed

+723
-338
lines changed

e2e/__tests__/hoist-jest.test.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,46 @@
1-
import * as path from 'path'
1+
import path from 'path'
2+
3+
import execa from 'execa'
24

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

6-
const DIR = path.resolve(__dirname, '..', 'hoist-jest')
8+
const { createBundle } = require('../../scripts/lib/bundle')
79

8-
beforeEach(() => {
9-
runNpmInstall(DIR)
10-
})
10+
const executeTest = (dirName: string): void => {
11+
test(`successfully runs the tests with isolatedModules: false`, () => {
12+
const { json } = runWithJson(dirName)
13+
14+
expect(json.success).toBe(true)
15+
})
16+
17+
test(`successfully runs the tests with isolatedModules true`, () => {
18+
const { json } = runWithJson(dirName, ['-c=jest-isolated.config.js'])
1119

12-
test('successfully runs the tests inside `hoist-jest/` with isolatedModules: false', () => {
13-
const { json } = runWithJson(DIR)
20+
expect(json.success).toBe(true)
21+
})
22+
}
1423

15-
expect(json.success).toBe(true)
24+
describe('non-ts-factory', () => {
25+
const DIR = path.resolve(__dirname, '..', 'hoist-jest', 'non-ts-factory')
26+
27+
beforeAll(() => {
28+
runNpmInstall(DIR)
29+
const bundle = createBundle()
30+
execa.sync('npm', ['install', '--no-package-lock', '--no-shrinkwrap', '--no-save', bundle], {
31+
cwd: DIR,
32+
})
33+
})
34+
35+
executeTest(DIR)
1636
})
1737

18-
test('successfully runs the tests inside `hoist-jest/` with isolatedModules true', () => {
19-
const { json } = runWithJson(DIR, ['-c=jest-isolated.config.js'])
38+
describe('ts-factory', () => {
39+
const DIR = path.resolve(__dirname, '..', 'hoist-jest', 'ts-factory')
40+
41+
beforeAll(() => {
42+
runNpmInstall(DIR)
43+
})
2044

21-
expect(json.success).toBe(true)
45+
executeTest(DIR)
2246
})

e2e/hoist-jest/__tests__/import-jest.test.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ aliasedJest.unmock('../__test_modules__/b')
1313
JestGlobals.jest.unmock('../__test_modules__/c')
1414

1515
// These will not be hoisted above imports
16-
// TODO: This is a bug to fix
17-
// {
18-
// const jest = {unmock: () => {}};
19-
// jest.unmock('../__test_modules__/d');
20-
// }
16+
{
17+
const jest = { unmock: () => {} }
18+
jest.unmock('../__test_modules__/d')
19+
}
2120

2221
// tests
2322

@@ -36,8 +35,7 @@ test('namespace import', () => {
3635
expect(c()).toBe('unmocked')
3736
})
3837

39-
// TODO: Enable this once the TODO above is fixed
40-
test.skip('fake jest, shadowed import', () => {
38+
test('fake jest, shadowed import', () => {
4139
expect(d._isMockFunction).toBe(true)
4240
expect(d()).toBe(undefined)
4341
})

e2e/hoist-jest/__tests__/integration.test.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React from 'react'
22
import Mocked from '../__test_modules__/mocked'
33
import Unmocked from '../__test_modules__/unmocked'
4-
// TODO: Enable when the 2nd TODO is fixed
5-
// import a from '../__test_modules__/a'
4+
import a from '../__test_modules__/a'
65
import b from '../__test_modules__/b'
76
import c from '../__test_modules__/c'
87
import d from '../__test_modules__/d'
@@ -48,18 +47,17 @@ jest.mock('virtual-module', () => 'kiwi', { virtual: true })
4847
jest.mock('has-flow-types', () => () => 3, {
4948
virtual: true,
5049
})
50+
jest.mock('../__test_modules__/a')
5151

5252
// These will not be hoisted
5353
jest.unmock('../__test_modules__/a').dontMock('../__test_modules__/b')
54-
// eslint-disable-next-line no-useless-concat
55-
jest.unmock('../__test_modules__/' + 'a')
5654
jest.dontMock('../__test_modules__/mocked')
57-
// TODO: This is a bug to fix
58-
// {
59-
// const jest = {unmock: () => {}};
60-
// // Would error (used before initialization) if hoisted to the top of the scope
61-
// jest.unmock('../__test_modules__/a');
62-
// }
55+
56+
{
57+
const jest = { unmock: () => {} }
58+
// Would error (used before initialization) if hoisted to the top of the scope
59+
jest.unmock('../__test_modules__/a')
60+
}
6361

6462
// This must not throw an error
6563
const myObject = { mock: () => {} }
@@ -122,9 +120,8 @@ describe('hoisting', () => {
122120
expect(Mocked._isMockFunction).toBe(true)
123121
expect(new Mocked().isMocked).toEqual(undefined)
124122

125-
// TODO: Enable this once the TODO above is fixed
126-
// expect(a._isMockFunction).toBe(true);
127-
// expect(a()).toEqual(undefined);
123+
expect(a._isMockFunction).toBe(true)
124+
expect(a()).toEqual(undefined)
128125

129126
expect(b._isMockFunction).toBe(true)
130127
expect(b()).toEqual(undefined)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ module.exports = {
88
},
99
},
1010
},
11+
roots: ['<rootDir>', '<rootDir>/../__tests__'],
12+
moduleNameMapper: {
13+
react$: '<rootDir>/node_modules/react',
14+
},
1115
transform: {
12-
'^.+.[tj]sx?$': '<rootDir>/../../dist/index.js',
16+
'^.+.[tj]sx?$': 'ts-jest',
1317
},
1418
}

e2e/hoist-jest/non-ts-factory/package-lock.json

Lines changed: 95 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"dependencies": {
3+
"react": "^17.0.2",
4+
"typescript": "~3.8.3"
5+
},
6+
"jest": {
7+
"automock": true,
8+
"globals": {
9+
"ts-jest": {
10+
"diagnostics": false,
11+
"tsconfig": {
12+
"allowJs": true
13+
}
14+
}
15+
},
16+
"roots": ["<rootDir>", "<rootDir>/../__tests__"],
17+
"moduleNameMapper": {
18+
"react$": "<rootDir>/node_modules/react"
19+
},
20+
"transform": {
21+
"^.+\\.[tj]sx?$": "ts-jest"
22+
}
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
automock: true,
3+
globals: {
4+
'ts-jest': {
5+
isolatedModules: true,
6+
tsconfig: {
7+
allowJs: true,
8+
},
9+
},
10+
},
11+
roots: ['<rootDir>', '<rootDir>/../__tests__'],
12+
moduleNameMapper: {
13+
react$: '<rootDir>/node_modules/react',
14+
},
15+
transform: {
16+
'^.+.[tj]sx?$': '<rootDir>/../../../dist/index.js',
17+
},
18+
}

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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
}
1313
}
1414
},
15+
"roots": ["<rootDir>", "<rootDir>/../__tests__"],
1516
"moduleNameMapper": {
16-
"@jest/globals": "<rootDir>/../../node_modules/@jest/globals",
17-
"@types/react": "<rootDir>/../../node_modules/@types/react"
17+
"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
}

e2e/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const runNpmInstall = (cwd: Config.Path, env?: Record<string, string>): R
4040
fs.writeFileSync(lockfilePath, '')
4141
}
4242

43-
return run(exists ? 'npm ci' : 'npm i', cwd, env)
43+
return run(exists ? 'npm ci --no-optional' : 'npm i --no-optional', cwd, env)
4444
}
4545

4646
const replaceTime = (str: string): string =>

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Object {
6060
Object {
6161
"factory": [Function],
6262
"name": "hoist-jest",
63-
"version": 1,
63+
"version": 2,
6464
},
6565
],
6666
}
@@ -74,7 +74,7 @@ Object {
7474
Object {
7575
"factory": [Function],
7676
"name": "hoist-jest",
77-
"version": 1,
77+
"version": 2,
7878
},
7979
Object {
8080
"factory": [Function],
@@ -95,7 +95,7 @@ Object {
9595
Object {
9696
"factory": [Function],
9797
"name": "hoist-jest",
98-
"version": 1,
98+
"version": 2,
9999
},
100100
],
101101
}
@@ -113,7 +113,7 @@ Object {
113113
Object {
114114
"factory": [Function],
115115
"name": "hoist-jest",
116-
"version": 1,
116+
"version": 2,
117117
},
118118
],
119119
}
@@ -127,7 +127,7 @@ Object {
127127
Object {
128128
"factory": [Function],
129129
"name": "hoist-jest",
130-
"version": 1,
130+
"version": 2,
131131
},
132132
Object {
133133
"factory": [Function],

0 commit comments

Comments
 (0)