Skip to content

Commit

Permalink
fix: hoist imports of @jest/globals correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Apr 13, 2020
1 parent c830517 commit 0b30ac2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion e2e/__tests__/babelPluginJestHoist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ beforeEach(() => {
run('yarn', DIR);
});

it('sucessfully runs the tests inside `babel-plugin-jest-hoist/`', () => {
it('successfully runs the tests inside `babel-plugin-jest-hoist/`', () => {
const {json} = runWithJson(DIR, ['--no-cache', '--coverage']);
expect(json.success).toBe(true);
expect(json.numTotalTestSuites).toBe(3);
Expand Down
18 changes: 18 additions & 0 deletions e2e/babel-plugin-jest-hoist/__tests__/importJest.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import {jest} from '@jest/globals';

// The virtual mock call below will be hoisted above this `require` call.
const virtualModule = require('virtual-module');

jest.mock('virtual-module', () => 'kiwi', {virtual: true});

test('works with virtual modules', () => {
expect(virtualModule).toBe('kiwi');
});
7 changes: 7 additions & 0 deletions packages/babel-plugin-jest-hoist/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ export default (): {visitor: Visitor} => {
path.node._blockHoist = Infinity;
}
},
ImportDeclaration(path) {
// this seems to hoist correctly before the `commonjs` plugin runs
if (path.node.source.value === '@jest/globals') {
// @ts-ignore: private, magical property
path.node._blockHoist = Infinity;
}
},
};

return {visitor};
Expand Down
17 changes: 9 additions & 8 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,6 @@ class Runtime {
modulePath = manualMock;
}

if (moduleName === '@jest/globals') {
// @ts-ignore: we don't care that it's not assignable to T
return this.getGlobalsForFile(from);
}

if (moduleName && this._resolver.isCoreModule(moduleName)) {
return this._requireCoreModule(moduleName);
}
Expand Down Expand Up @@ -526,12 +521,18 @@ class Runtime {
};
}

requireModuleOrMock(from: Config.Path, moduleName: string): unknown {
requireModuleOrMock<T = unknown>(from: Config.Path, moduleName: string): T {
// this module is unmockable
if (moduleName === '@jest/globals') {
// @ts-ignore: we don't care that it's not assignable to T
return this.getGlobalsForFile(from);
}

try {
if (this._shouldMock(from, moduleName)) {
return this.requireMock(from, moduleName);
return this.requireMock<T>(from, moduleName);
} else {
return this.requireModule(from, moduleName);
return this.requireModule<T>(from, moduleName);
}
} catch (e) {
const moduleNotFound = Resolver.tryCastModuleNotFoundError(e);
Expand Down
1 change: 1 addition & 0 deletions scripts/babel-plugin-jest-replace-ts-require-assignment.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
Expand Down

0 comments on commit 0b30ac2

Please sign in to comment.