From 4ca10af2a3904e5254ae793f6d0a4ae2e9c4454b Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 16 Sep 2021 14:15:19 +0200 Subject: [PATCH 1/2] fix: properly return mocks when using jest.isolatedModules --- CHANGELOG.md | 2 + e2e/__tests__/isolateModules.test.ts | 61 ++++++++++++++++++++++++++++ packages/jest-runtime/src/index.ts | 10 +++-- 3 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 e2e/__tests__/isolateModules.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 060fa7e34b7c..9b9ab204e393 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Fixes +- `[jest-runtime]` Fix regression when using `jest.isolateModules` and mocks + ### Chore & Maintenance ### Performance diff --git a/e2e/__tests__/isolateModules.test.ts b/e2e/__tests__/isolateModules.test.ts new file mode 100644 index 000000000000..a8889b842606 --- /dev/null +++ b/e2e/__tests__/isolateModules.test.ts @@ -0,0 +1,61 @@ +/** + * 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 {wrap} from 'jest-snapshot-serializer-raw'; +import { + cleanup, + createEmptyPackage, + extractSummary, + 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)!(); From 8334fbff61a5b7c78361211b33051f6a591834a8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 16 Sep 2021 14:16:25 +0200 Subject: [PATCH 2/2] link in changelog --- CHANGELOG.md | 2 +- e2e/__tests__/isolateModules.test.ts | 8 +------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b9ab204e393..516cad9be717 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixes -- `[jest-runtime]` Fix regression when using `jest.isolateModules` and mocks +- `[jest-runtime]` Fix regression when using `jest.isolateModules` and mocks ([#11882](https://github.com/facebook/jest/pull/11882)) ### Chore & Maintenance diff --git a/e2e/__tests__/isolateModules.test.ts b/e2e/__tests__/isolateModules.test.ts index a8889b842606..25ecd76fac0f 100644 --- a/e2e/__tests__/isolateModules.test.ts +++ b/e2e/__tests__/isolateModules.test.ts @@ -7,13 +7,7 @@ import {tmpdir} from 'os'; import * as path from 'path'; -import {wrap} from 'jest-snapshot-serializer-raw'; -import { - cleanup, - createEmptyPackage, - extractSummary, - writeFiles, -} from '../Utils'; +import {cleanup, createEmptyPackage, writeFiles} from '../Utils'; import runJest from '../runJest'; const DIR = path.resolve(tmpdir(), 'isolate-modules.test');