Skip to content

Commit 4ab5ff3

Browse files
authored
refactor(transformer): correctly detect jest import for hoist-jest (#1955)
In #1937, we added support for `@jest/globals`. However, the logic to detect jest import name bindings wasn't correct. It failed with the case `import {test,jest} from '@jest/globals'`. This PR is to fix that issue.
1 parent 590341a commit 4ab5ff3

5 files changed

Lines changed: 19 additions & 8 deletions

File tree

e2e/__cases__/hoisting/import-jest.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {jest} from '@jest/globals'
1+
import {test,jest} from '@jest/globals'
22
import {jest as aliasedJest} from '@jest/globals'
33
import * as JestGlobals from '@jest/globals'
44

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`cacheKey should be a string 1`] = `"{\\"digest\\":\\"a0d51ca854194df8191d0e65c0ca4730f510f332\\",\\"jest\\":{\\"__backported\\":true,\\"globals\\":{}},\\"transformers\\":[\\"hoisting-jest-mock@3\\"],\\"tsJest\\":{\\"compiler\\":\\"typescript\\",\\"diagnostics\\":{\\"ignoreCodes\\":[6059,18002,18003],\\"pretty\\":true,\\"throws\\":true},\\"isolatedModules\\":false,\\"packageJson\\":{\\"kind\\":\\"file\\"},\\"transformers\\":{},\\"tsConfig\\":{\\"kind\\":\\"file\\",\\"value\\":\\"\\"}},\\"tsconfig\\":{\\"options\\":{\\"configFilePath\\":\\"\\",\\"declaration\\":false,\\"inlineSourceMap\\":false,\\"inlineSources\\":true,\\"module\\":1,\\"noEmit\\":false,\\"removeComments\\":false,\\"sourceMap\\":true,\\"target\\":1,\\"types\\":[]},\\"raw\\":{\\"compileOnSave\\":false,\\"compilerOptions\\":{\\"composite\\":true,\\"declaration\\":true,\\"types\\":[]},\\"exclude\\":[\\"foo/**/*\\"],\\"include\\":[\\"bar/**/*\\"]}}}"`;
3+
exports[`cacheKey should be a string 1`] = `"{\\"digest\\":\\"a0d51ca854194df8191d0e65c0ca4730f510f332\\",\\"jest\\":{\\"__backported\\":true,\\"globals\\":{}},\\"transformers\\":[\\"hoisting-jest-mock@4\\"],\\"tsJest\\":{\\"compiler\\":\\"typescript\\",\\"diagnostics\\":{\\"ignoreCodes\\":[6059,18002,18003],\\"pretty\\":true,\\"throws\\":true},\\"isolatedModules\\":false,\\"packageJson\\":{\\"kind\\":\\"file\\"},\\"transformers\\":{},\\"tsConfig\\":{\\"kind\\":\\"file\\",\\"value\\":\\"\\"}},\\"tsconfig\\":{\\"options\\":{\\"configFilePath\\":\\"\\",\\"declaration\\":false,\\"inlineSourceMap\\":false,\\"inlineSources\\":true,\\"module\\":1,\\"noEmit\\":false,\\"removeComments\\":false,\\"sourceMap\\":true,\\"target\\":1,\\"types\\":[]},\\"raw\\":{\\"compileOnSave\\":false,\\"compilerOptions\\":{\\"composite\\":true,\\"declaration\\":true,\\"types\\":[]},\\"exclude\\":[\\"foo/**/*\\"],\\"include\\":[\\"bar/**/*\\"]}}}"`;
44

55
exports[`isTestFile should return a boolean value whether the file matches test pattern 1`] = `true`;
66

@@ -21,7 +21,7 @@ Object {
2121
"name": undefined,
2222
},
2323
"transformers": Array [
24-
"hoisting-jest-mock@3",
24+
"hoisting-jest-mock@4",
2525
],
2626
"tsJest": Object {
2727
"babelConfig": undefined,

src/transformers/__snapshots__/hoist-jest.spec.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
exports[`hoisting should hoist correctly jest methods 1`] = `
44
"\\"use strict\\";
55
Object.defineProperty(exports, \\"__esModule\\", { value: true });
6+
var globals_1 = require(\\"@jest/globals\\");
67
// These will all be hoisted above imports
78
jest.unmock('react');
89
jest.deepUnmock('../__test_modules__/Unmocked');
@@ -69,6 +70,7 @@ console.log(d_1.default);
6970
console.log(e);
7071
console.log(virtualModule);
7172
console.log(jestBackticks_1.default);
73+
console.log(globals_1.it);
7274
"
7375
`;
7476

@@ -95,5 +97,6 @@ console.log(a_1.default);
9597
console.log(b_1.default);
9698
console.log(c_1.default);
9799
console.log(d_1.default);
100+
console.log(globals_1.it);
98101
"
99102
`;

src/transformers/hoist-jest.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const CODE_WITH_HOISTING_NO_JEST_GLOBALS = `
77
import React from 'react'
88
import Unmocked from '../__test_modules__/Unmocked'
99
import Mocked from '../__test_modules__/Mocked'
10+
import {it} from '@jest/globals'
1011
import a from '../__test_modules__/a'
1112
import b from '../__test_modules__/b'
1213
import c from '../__test_modules__/c'
@@ -81,12 +82,13 @@ console.log(d)
8182
console.log(e)
8283
console.log(virtualModule)
8384
console.log(jestBackticks)
85+
console.log(it)
8486
`
8587
const CODE_WITH_HOISTING_HAS_JEST_GLOBALS = `
8688
import a from '../__test_modules__/a'
8789
import b from '../__test_modules__/b'
8890
89-
import {jest} from '@jest/globals'
91+
import {it, jest} from '@jest/globals'
9092
import {jest as aliasedJest} from '@jest/globals'
9193
import * as JestGlobals from '@jest/globals'
9294
@@ -110,6 +112,7 @@ const CODE_WITH_HOISTING_HAS_JEST_GLOBALS = `
110112
console.log(b)
111113
console.log(c)
112114
console.log(d)
115+
console.log(it)
113116
`
114117

115118
const logger = testing.createLoggerMock()

src/transformers/hoist-jest.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const name = 'hoisting-jest-mock'
2929
*
3030
* @internal
3131
*/
32-
export const version = 3
32+
export const version = 4
3333

3434
/**
3535
* The factory of hoisting transformer factory
@@ -147,9 +147,14 @@ export function factory(cs: ConfigSet): (ctx: TransformationContext) => Transfor
147147
ts.isNamedImports(resultNode.importClause.namedBindings))
148148
) {
149149
const { namedBindings } = resultNode.importClause
150-
importNames.push(
151-
ts.isNamespaceImport(namedBindings) ? namedBindings.name.text : namedBindings.elements[0].name.text,
152-
)
150+
const jestImportName = ts.isNamespaceImport(namedBindings)
151+
? namedBindings.name.text
152+
: namedBindings.elements.find(
153+
(element) => element.name.text === JEST_GLOBAL_NAME || element.propertyName?.text === JEST_GLOBAL_NAME,
154+
)?.name.text
155+
if (jestImportName) {
156+
importNames.push(jestImportName)
157+
}
153158
}
154159
// check if we have something to hoist in this level
155160
if (hoisted[level] && hoisted[level].length) {

0 commit comments

Comments
 (0)