Skip to content

Commit 0b30ac2

Browse files
committed
fix: hoist imports of @jest/globals correctly
1 parent c830517 commit 0b30ac2

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

e2e/__tests__/babelPluginJestHoist.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ beforeEach(() => {
1515
run('yarn', DIR);
1616
});
1717

18-
it('sucessfully runs the tests inside `babel-plugin-jest-hoist/`', () => {
18+
it('successfully runs the tests inside `babel-plugin-jest-hoist/`', () => {
1919
const {json} = runWithJson(DIR, ['--no-cache', '--coverage']);
2020
expect(json.success).toBe(true);
2121
expect(json.numTotalTestSuites).toBe(3);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
import {jest} from '@jest/globals';
10+
11+
// The virtual mock call below will be hoisted above this `require` call.
12+
const virtualModule = require('virtual-module');
13+
14+
jest.mock('virtual-module', () => 'kiwi', {virtual: true});
15+
16+
test('works with virtual modules', () => {
17+
expect(virtualModule).toBe('kiwi');
18+
});

packages/babel-plugin-jest-hoist/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ export default (): {visitor: Visitor} => {
183183
path.node._blockHoist = Infinity;
184184
}
185185
},
186+
ImportDeclaration(path) {
187+
// this seems to hoist correctly before the `commonjs` plugin runs
188+
if (path.node.source.value === '@jest/globals') {
189+
// @ts-ignore: private, magical property
190+
path.node._blockHoist = Infinity;
191+
}
192+
},
186193
};
187194

188195
return {visitor};

packages/jest-runtime/src/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,6 @@ class Runtime {
331331
modulePath = manualMock;
332332
}
333333

334-
if (moduleName === '@jest/globals') {
335-
// @ts-ignore: we don't care that it's not assignable to T
336-
return this.getGlobalsForFile(from);
337-
}
338-
339334
if (moduleName && this._resolver.isCoreModule(moduleName)) {
340335
return this._requireCoreModule(moduleName);
341336
}
@@ -526,12 +521,18 @@ class Runtime {
526521
};
527522
}
528523

529-
requireModuleOrMock(from: Config.Path, moduleName: string): unknown {
524+
requireModuleOrMock<T = unknown>(from: Config.Path, moduleName: string): T {
525+
// this module is unmockable
526+
if (moduleName === '@jest/globals') {
527+
// @ts-ignore: we don't care that it's not assignable to T
528+
return this.getGlobalsForFile(from);
529+
}
530+
530531
try {
531532
if (this._shouldMock(from, moduleName)) {
532-
return this.requireMock(from, moduleName);
533+
return this.requireMock<T>(from, moduleName);
533534
} else {
534-
return this.requireModule(from, moduleName);
535+
return this.requireModule<T>(from, moduleName);
535536
}
536537
} catch (e) {
537538
const moduleNotFound = Resolver.tryCastModuleNotFoundError(e);

scripts/babel-plugin-jest-replace-ts-require-assignment.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
/**
23
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
34
*

0 commit comments

Comments
 (0)