From d0bcb507667f24ec4acca055f1f8dee929d6852c Mon Sep 17 00:00:00 2001 From: Michael Blaszczyk Date: Tue, 30 Apr 2019 16:17:43 +1000 Subject: [PATCH 1/2] Fix virtual mocks not being unmockable --- .../runtime_require_module_or_mock.test.js | 20 +++++++++++++++++++ packages/jest-runtime/src/index.ts | 5 ----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/jest-runtime/src/__tests__/runtime_require_module_or_mock.test.js b/packages/jest-runtime/src/__tests__/runtime_require_module_or_mock.test.js index 1dcf0280ea8b..a92f4a31769b 100644 --- a/packages/jest-runtime/src/__tests__/runtime_require_module_or_mock.test.js +++ b/packages/jest-runtime/src/__tests__/runtime_require_module_or_mock.test.js @@ -176,6 +176,26 @@ it('unmocks modules in config.unmockedModulePathPatterns for tests with automock expect(moduleData.isUnmocked()).toBe(true); })); +it('unmocks virtual mocks after they have been mocked previously', () => + createRuntime(__filename).then(runtime => { + const root = runtime.requireModule(runtime.__mockRootPath); + + const mockImpl = {foo: 'bar'}; + root.jest.mock('my-virtual-module', () => mockImpl, {virtual: true}); + + expect( + runtime.requireModuleOrMock(runtime.__mockRootPath, 'my-virtual-module'), + ).toEqual(mockImpl); + + root.jest.unmock('my-virtual-module'); + + expect(() => { + runtime.requireModuleOrMock(runtime.__mockRootPath, 'my-virtual-module'); + }).toThrowError( + new Error("Cannot find module 'my-virtual-module' from 'root.js'"), + ); + })); + describe('resetModules', () => { it('resets all the modules', () => createRuntime(__filename, { diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 917b47e377a4..36107a5d68ce 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -820,11 +820,6 @@ class Runtime { } private _shouldMock(from: Config.Path, moduleName: string) { - const mockPath = this._resolver.getModulePath(from, moduleName); - if (mockPath in this._virtualMocks) { - return true; - } - const explicitShouldMock = this._explicitShouldMock; const moduleID = this._resolver.getModuleID( this._virtualMocks, From 5510a91aec38061660c6fc97298b580b9d9581f8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 30 Apr 2019 08:47:09 +0200 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64cc33091f84..79c62fb8261d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ - `[jest-environment-jsdom]` Re-declare global prototype of JSDOMEnvironment ([#8352](https://github.com/facebook/jest/pull/8352)) - `[jest-snapshot]` Handle arrays when merging snapshots ([#7089](https://github.com/facebook/jest/pull/7089)) - `[expect]` Extract names of async and generator functions ([#8362](https://github.com/facebook/jest/pull/8362)) +- `[jest-runtime]` Fix virtual mocks not being unmockable after previously being mocked ([#8396](https://github.com/facebook/jest/pull/8396)) ### Chore & Maintenance