Skip to content

Commit 391c4e2

Browse files
BlaszSimenB
authored andcommitted
Fix virtual mocks not being unmockable after previously being mocked (#8396)
1 parent 05b8382 commit 391c4e2

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- `[jest-environment-jsdom]` Re-declare global prototype of JSDOMEnvironment ([#8352](https://github.com/facebook/jest/pull/8352))
2424
- `[jest-snapshot]` Handle arrays when merging snapshots ([#7089](https://github.com/facebook/jest/pull/7089))
2525
- `[expect]` Extract names of async and generator functions ([#8362](https://github.com/facebook/jest/pull/8362))
26+
- `[jest-runtime]` Fix virtual mocks not being unmockable after previously being mocked ([#8396](https://github.com/facebook/jest/pull/8396))
2627

2728
### Chore & Maintenance
2829

packages/jest-runtime/src/__tests__/runtime_require_module_or_mock.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,26 @@ it('unmocks modules in config.unmockedModulePathPatterns for tests with automock
176176
expect(moduleData.isUnmocked()).toBe(true);
177177
}));
178178

179+
it('unmocks virtual mocks after they have been mocked previously', () =>
180+
createRuntime(__filename).then(runtime => {
181+
const root = runtime.requireModule(runtime.__mockRootPath);
182+
183+
const mockImpl = {foo: 'bar'};
184+
root.jest.mock('my-virtual-module', () => mockImpl, {virtual: true});
185+
186+
expect(
187+
runtime.requireModuleOrMock(runtime.__mockRootPath, 'my-virtual-module'),
188+
).toEqual(mockImpl);
189+
190+
root.jest.unmock('my-virtual-module');
191+
192+
expect(() => {
193+
runtime.requireModuleOrMock(runtime.__mockRootPath, 'my-virtual-module');
194+
}).toThrowError(
195+
new Error("Cannot find module 'my-virtual-module' from 'root.js'"),
196+
);
197+
}));
198+
179199
describe('resetModules', () => {
180200
it('resets all the modules', () =>
181201
createRuntime(__filename, {

packages/jest-runtime/src/index.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -820,11 +820,6 @@ class Runtime {
820820
}
821821

822822
private _shouldMock(from: Config.Path, moduleName: string) {
823-
const mockPath = this._resolver.getModulePath(from, moduleName);
824-
if (mockPath in this._virtualMocks) {
825-
return true;
826-
}
827-
828823
const explicitShouldMock = this._explicitShouldMock;
829824
const moduleID = this._resolver.getModuleID(
830825
this._virtualMocks,

0 commit comments

Comments
 (0)