Skip to content

Commit c557af1

Browse files
committed
fix: pass modulePath instead of moduleName to onGenerateMock callback (#15429)
1 parent bacb7de commit c557af1

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

docs/JestObjectAPI.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ Registers a callback function that is invoked whenever Jest generates a mock for
534534

535535
Parameters for callback:
536536

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

540540
Behaviour:
@@ -543,9 +543,9 @@ Behaviour:
543543
- 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.
544544

545545
```js
546-
jest.onGenerateMock((moduleName, moduleMock) => {
546+
jest.onGenerateMock((modulePath, moduleMock) => {
547547
// Inspect the module name and decide how to transform the mock
548-
if (moduleName.includes('Database')) {
548+
if (modulePath.includes('Database')) {
549549
// For demonstration, let's replace a method with our own custom mock
550550
moduleMock.connect = jest.fn().mockImplementation(() => {
551551
console.log('Connected to mock DB');

packages/jest-environment/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,14 @@ export interface Jest {
227227
now(): number;
228228
/**
229229
* Registers a callback function that is invoked whenever a mock is generated for a module.
230-
* This callback is passed the module name and the newly created mock object, and must return
230+
* This callback is passed the module path and the newly created mock object, and must return
231231
* the (potentially modified) mock object.
232232
*
233233
* If multiple callbacks are registered, they will be called in the order they were added.
234234
* Each callback receives the result of the previous callback as the `moduleMock` parameter,
235235
* making it possible to apply sequential transformations.
236236
*/
237-
onGenerateMock<T>(cb: (moduleName: string, moduleMock: T) => T): Jest;
237+
onGenerateMock<T>(cb: (modulePath: string, moduleMock: T) => T): Jest;
238238
/**
239239
* Replaces property on an object with another value.
240240
*

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

+16-10
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ describe('Runtime', () => {
155155
runtime.requireModuleOrMock(runtime.__mockRootPath, 'RegularModule'),
156156
).toEqual(mockReference);
157157
expect(onGenerateMock).toHaveBeenCalledWith(
158-
'RegularModule',
158+
expect.stringMatching(/[/\\]test_root[/\\]RegularModule\.js$/),
159159
expect.anything(),
160160
);
161161

@@ -201,17 +201,23 @@ describe('Runtime', () => {
201201
value: 4,
202202
});
203203
expect(onGenerateMock1).toHaveBeenCalledWith(
204-
'RegularModule',
204+
expect.stringMatching(/[/\\]test_root[/\\]RegularModule\.js$/),
205205
expect.anything(),
206206
);
207-
expect(onGenerateMock2).toHaveBeenCalledWith('RegularModule', {
208-
isMock: true,
209-
value: 1,
210-
});
211-
expect(onGenerateMock3).toHaveBeenCalledWith('RegularModule', {
212-
isMock: true,
213-
value: 2,
214-
});
207+
expect(onGenerateMock2).toHaveBeenCalledWith(
208+
expect.stringMatching(/[/\\]test_root[/\\]RegularModule\.js$/),
209+
{
210+
isMock: true,
211+
value: 1,
212+
},
213+
);
214+
expect(onGenerateMock3).toHaveBeenCalledWith(
215+
expect.stringMatching(/[/\\]test_root[/\\]RegularModule\.js$/),
216+
{
217+
isMock: true,
218+
value: 2,
219+
},
220+
);
215221
});
216222
});
217223
});

packages/jest-runtime/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1940,7 +1940,7 @@ export default class Runtime {
19401940
);
19411941

19421942
for (const onGenerateMock of this._onGenerateMock) {
1943-
moduleMock = onGenerateMock(moduleName, moduleMock);
1943+
moduleMock = onGenerateMock(modulePath, moduleMock);
19441944
}
19451945

19461946
return moduleMock;

0 commit comments

Comments
 (0)