Skip to content

Commit 3137c06

Browse files
authored
fix: pass modulePath instead of moduleName to onGenerateMock callback (#15429) (#15482)
1 parent 9ea3e0f commit 3137c06

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
- `[jest-runtime]` Support `import.meta.resolve` ([#14930](https://github.com/jestjs/jest/pull/14930))
4444
- `[jest-runtime]` [**BREAKING**] Make it mandatory to pass `globalConfig` to the `Runtime` constructor ([#15044](https://github.com/jestjs/jest/pull/15044))
4545
- `[jest-runtime]` Add `unstable_unmockModule` ([#15080](https://github.com/jestjs/jest/pull/15080))
46-
- `[jest-runtime]` Add `onGenerateMock` transformer callback for auto generated callbacks ([#15433](https://github.com/jestjs/jest/pull/15433))
46+
- `[jest-runtime]` Add `onGenerateMock` transformer callback for auto generated callbacks ([#15433](https://github.com/jestjs/jest/pull/15433) & [#15482](https://github.com/jestjs/jest/pull/15482))
4747
- `[@jest/schemas]` Upgrade `@sinclair/typebox` to v0.34 ([#15450](https://github.com/jestjs/jest/pull/15450))
4848
- `[@jest/types]` `test.each()`: Accept a readonly (`as const`) table properly ([#14565](https://github.com/jestjs/jest/pull/14565))
4949
- `[@jest/types]` Improve argument type inference passed to `test` and `describe` callback functions from `each` tables ([#14920](https://github.com/jestjs/jest/pull/14920))

docs/JestObjectAPI.md

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 16 additions & 10 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1952,7 +1952,7 @@ export default class Runtime {
19521952
);
19531953

19541954
for (const onGenerateMock of this._onGenerateMock) {
1955-
moduleMock = onGenerateMock(moduleName, moduleMock);
1955+
moduleMock = onGenerateMock(modulePath, moduleMock);
19561956
}
19571957

19581958
return moduleMock;

0 commit comments

Comments
 (0)