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: pass modulePath instead of moduleName to onGenerateMock callback (#15429) #15482

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions docs/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ Registers a callback function that is invoked whenever Jest generates a mock for

Parameters for callback:

1. `moduleName: string` - The name of the module that is being mocked.
1. `modulePath: string` - The absolute path to the module that is being mocked.
2. `moduleMock: T` - The mock object that Jest has generated for the module. This object can be modified or replaced before returning.

Behaviour:
Expand All @@ -543,9 +543,9 @@ Behaviour:
- Each callback receives the output of the previous callback as its `moduleMock`. This makes it possible to apply multiple layers of transformations to the same mock.

```js
jest.onGenerateMock((moduleName, moduleMock) => {
jest.onGenerateMock((modulePath, moduleMock) => {
// Inspect the module name and decide how to transform the mock
if (moduleName.includes('Database')) {
if (modulePath.includes('Database')) {
// For demonstration, let's replace a method with our own custom mock
moduleMock.connect = jest.fn().mockImplementation(() => {
console.log('Connected to mock DB');
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 @@ -227,14 +227,14 @@ export interface Jest {
now(): number;
/**
* Registers a callback function that is invoked whenever a mock is generated for a module.
* This callback is passed the module name and the newly created mock object, and must return
* This callback is passed the module path and the newly created mock object, and must return
* the (potentially modified) mock object.
*
* If multiple callbacks are registered, they will be called in the order they were added.
* Each callback receives the result of the previous callback as the `moduleMock` parameter,
* making it possible to apply sequential transformations.
*/
onGenerateMock<T>(cb: (moduleName: string, moduleMock: T) => T): Jest;
onGenerateMock<T>(cb: (modulePath: string, moduleMock: T) => T): Jest;
/**
* Replaces property on an object with another value.
*
Expand Down
26 changes: 16 additions & 10 deletions packages/jest-runtime/src/__tests__/runtime_mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe('Runtime', () => {
runtime.requireModuleOrMock(runtime.__mockRootPath, 'RegularModule'),
).toEqual(mockReference);
expect(onGenerateMock).toHaveBeenCalledWith(
'RegularModule',
expect.stringMatching(/[/\\]test_root[/\\]RegularModule\.js$/),
expect.anything(),
);

Expand Down Expand Up @@ -201,17 +201,23 @@ describe('Runtime', () => {
value: 4,
});
expect(onGenerateMock1).toHaveBeenCalledWith(
'RegularModule',
expect.stringMatching(/[/\\]test_root[/\\]RegularModule\.js$/),
expect.anything(),
);
expect(onGenerateMock2).toHaveBeenCalledWith('RegularModule', {
isMock: true,
value: 1,
});
expect(onGenerateMock3).toHaveBeenCalledWith('RegularModule', {
isMock: true,
value: 2,
});
expect(onGenerateMock2).toHaveBeenCalledWith(
expect.stringMatching(/[/\\]test_root[/\\]RegularModule\.js$/),
{
isMock: true,
value: 1,
},
);
expect(onGenerateMock3).toHaveBeenCalledWith(
expect.stringMatching(/[/\\]test_root[/\\]RegularModule\.js$/),
{
isMock: true,
value: 2,
},
);
});
});
});
2 changes: 1 addition & 1 deletion packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,7 @@ export default class Runtime {
);

for (const onGenerateMock of this._onGenerateMock) {
moduleMock = onGenerateMock(moduleName, moduleMock);
moduleMock = onGenerateMock(modulePath, moduleMock);
}

return moduleMock;
Expand Down
Loading