From 0b30ac2f8d2fa3a51f6571bb680843575f3c16a3 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 13 Apr 2020 13:38:56 +0200 Subject: [PATCH] fix: hoist imports of `@jest/globals` correctly --- e2e/__tests__/babelPluginJestHoist.test.ts | 2 +- .../__tests__/importJest.test.js | 18 ++++++++++++++++++ packages/babel-plugin-jest-hoist/src/index.ts | 7 +++++++ packages/jest-runtime/src/index.ts | 17 +++++++++-------- ...lugin-jest-replace-ts-require-assignment.js | 1 + 5 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 e2e/babel-plugin-jest-hoist/__tests__/importJest.test.js diff --git a/e2e/__tests__/babelPluginJestHoist.test.ts b/e2e/__tests__/babelPluginJestHoist.test.ts index 2f7cbd6aafdc..5cb0ecf291af 100644 --- a/e2e/__tests__/babelPluginJestHoist.test.ts +++ b/e2e/__tests__/babelPluginJestHoist.test.ts @@ -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); diff --git a/e2e/babel-plugin-jest-hoist/__tests__/importJest.test.js b/e2e/babel-plugin-jest-hoist/__tests__/importJest.test.js new file mode 100644 index 000000000000..1a6da15da042 --- /dev/null +++ b/e2e/babel-plugin-jest-hoist/__tests__/importJest.test.js @@ -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'); +}); diff --git a/packages/babel-plugin-jest-hoist/src/index.ts b/packages/babel-plugin-jest-hoist/src/index.ts index 43f19e39c52f..0554079e7a06 100644 --- a/packages/babel-plugin-jest-hoist/src/index.ts +++ b/packages/babel-plugin-jest-hoist/src/index.ts @@ -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}; diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index d80cfd780b8e..16ca19aa6636 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -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); } @@ -526,12 +521,18 @@ class Runtime { }; } - requireModuleOrMock(from: Config.Path, moduleName: string): unknown { + requireModuleOrMock(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(from, moduleName); } else { - return this.requireModule(from, moduleName); + return this.requireModule(from, moduleName); } } catch (e) { const moduleNotFound = Resolver.tryCastModuleNotFoundError(e); diff --git a/scripts/babel-plugin-jest-replace-ts-require-assignment.js b/scripts/babel-plugin-jest-replace-ts-require-assignment.js index 72451255239b..51fca9ef3f9b 100644 --- a/scripts/babel-plugin-jest-replace-ts-require-assignment.js +++ b/scripts/babel-plugin-jest-replace-ts-require-assignment.js @@ -1,3 +1,4 @@ + /** * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. *