Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix virtual mocks not being unmockable after previously being mocked #8396

Merged
merged 2 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
5 changes: 0 additions & 5 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down