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

feat(runtime): add jest.mockModule #10976

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion e2e/native-esm/__tests__/native-esm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ test('require of ESM should throw correct error', () => {
});

test('can mock module', async () => {
jestObject.unstable_mockModule('../mockedModule.mjs', () => ({foo: 'bar'}), {
jestObject.mockModule('../mockedModule.mjs', () => ({foo: 'bar'}), {
virtual: true,
});

Expand Down
4 changes: 2 additions & 2 deletions packages/jest-environment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ export interface Jest {
/**
* Mocks a module with the provided module factory when it is being imported.
*/
unstable_mockModule<T = unknown>(
mockModule<T = unknown>(
moduleName: string,
moduleFactory: () => Promise<T> | T,
moduleFactory?: () => Promise<T> | T,
options?: {virtual?: boolean},
): Jest;
/**
Expand Down
18 changes: 13 additions & 5 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,8 @@ export default class Runtime {
async unstable_importModule(
from: string,
moduleName?: string,
// TODO: implement this
_isImportActual = false,
): Promise<void> {
invariant(
runtimeSupportsVmModules,
Expand Down Expand Up @@ -2041,16 +2043,22 @@ export default class Runtime {
this.setMock(from, moduleName, mockFactory, options);
return jestObject;
};
const mockModule: Jest['unstable_mockModule'] = (
const mockModule: Jest['mockModule'] = (
moduleName,
mockFactory,
options,
) => {
if (typeof mockFactory !== 'function') {
throw new Error('`unstable_mockModule` must be passed a mock factory');
if (mockFactory !== undefined) {
this.setModuleMock(from, moduleName, mockFactory, options);
return jestObject;
}

this.setModuleMock(from, moduleName, mockFactory, options);
const moduleID = this._resolver.getModuleID(
this._virtualMocks,
from,
moduleName,
);
this._explicitShouldMockModule.set(moduleID, true);
return jestObject;
};
const clearAllMocks = () => {
Expand Down Expand Up @@ -2161,6 +2169,7 @@ export default class Runtime {
isolateModules,
mock,
mocked,
mockModule,
requireActual: this.requireActual.bind(this, from),
requireMock: this.requireMock.bind(this, from),
resetAllMocks,
Expand Down Expand Up @@ -2197,7 +2206,6 @@ export default class Runtime {
setTimeout,
spyOn,
unmock,
unstable_mockModule: mockModule,
useFakeTimers,
useRealTimers,
};
Expand Down