From 651eb37a4843aaa72e2ca63f4191c1effce947a2 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 17 Sep 2021 14:06:23 +0200 Subject: [PATCH] fix: properly return mocks when using jest.isolatedModules (#11882) --- CHANGELOG.md | 2 + e2e/__tests__/isolateModules.test.ts | 55 ++++++++++++++++++++++++++++ packages/jest-runtime/src/index.ts | 10 +++-- 3 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 e2e/__tests__/isolateModules.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 060fa7e34b7c..516cad9be717 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-runtime]` Fix regression when using `jest.isolateModules` and mocks ([#11882](https://github.com/facebook/jest/pull/11882)) + ### Chore & Maintenance ### Performance diff --git a/e2e/__tests__/isolateModules.test.ts b/e2e/__tests__/isolateModules.test.ts new file mode 100644 index 000000000000..25ecd76fac0f --- /dev/null +++ b/e2e/__tests__/isolateModules.test.ts @@ -0,0 +1,55 @@ +/** + * 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 {tmpdir} from 'os'; +import * as path from 'path'; +import {cleanup, createEmptyPackage, writeFiles} from '../Utils'; +import runJest from '../runJest'; + +const DIR = path.resolve(tmpdir(), 'isolate-modules.test'); + +beforeEach(() => { + cleanup(DIR); + createEmptyPackage(DIR); +}); + +afterAll(() => cleanup(DIR)); + +test('works with mocks', () => { + writeFiles(DIR, { + 'config.js': ` + module.exports.getBoolean = function getBoolean(variableName) { + return false; + } + `, + 'read.js': ` + const {getBoolean} = require('./config'); + + const value = getBoolean('foo'); + console.log("was " + value); + `, + 'test.js': ` + jest.mock('./config'); + const config = require('./config'); + + test('dummy test', () => { + const configGetMock = config.getBoolean.mockImplementation(() => { + return true; + }); + + jest.isolateModules(() => { + require("./read"); + }); + + expect(configGetMock).toBeCalledTimes(1); + }) + `, + }); + const {exitCode} = runJest(DIR); + + expect(exitCode).toBe(0); +}); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index c3ae4051bca4..1eb74389022c 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -861,12 +861,14 @@ export default class Runtime { {conditions: this.cjsConditions}, ); - const mockRegistry = this._isolatedMockRegistry || this._mockRegistry; - - if (mockRegistry.get(moduleID)) { - return mockRegistry.get(moduleID); + if (this._isolatedMockRegistry?.has(moduleID)) { + return this._isolatedMockRegistry.get(moduleID); + } else if (this._mockRegistry.has(moduleID)) { + return this._mockRegistry.get(moduleID); } + const mockRegistry = this._isolatedMockRegistry || this._mockRegistry; + if (this._mockFactories.has(moduleID)) { // has check above makes this ok const module = this._mockFactories.get(moduleID)!();