You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1
Original file line number
Diff line number
Diff line change
@@ -46,6 +46,7 @@
46
46
-`[@jest/util-snapshot]` Extract utils used by tooling from `jest-snapshot` into its own package ([#15095](https://github.com/facebook/jest/pull/15095))
47
47
-`[pretty-format]`[**BREAKING**] Do not render empty string children (`''`) in React plugin ([#14470](https://github.com/facebook/jest/pull/14470))
48
48
-`[jest-each]` Introduce `%$` option to add number of the test to its title ([#14710](https://github.com/jestjs/jest/pull/14710))
49
+
-`[jest-runtime]` Add `onGenerateMock` transformer callback for auto generated callbacks ([#15433](https://github.com/jestjs/jest/pull/15433))
Returns a mock module instead of the actual module, bypassing all checks on whether the module should be required normally or not.
530
530
531
+
### `jest.onGenerateMock(cb)`
532
+
533
+
Registers a callback function that is invoked whenever Jest generates a mock for a module.
534
+
This callback allows you to modify the mock before it is returned to the rest of your tests.
535
+
536
+
Parameters for callback:
537
+
1.`moduleName: string` - The name of the module that is being mocked.
538
+
2.`moduleMock: T` - The mock object that Jest has generated for the module. This object can be modified or replaced before returning.
539
+
540
+
Behaviour:
541
+
542
+
- If multiple callbacks are registered via consecutive `onGenerateMock` calls, they will be invoked **in the order they were added**.
543
+
- 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.
544
+
545
+
```js
546
+
jest.onGenerateMock((moduleName, moduleMock) => {
547
+
// Inspect the module name and decide how to transform the mock
548
+
if (moduleName.includes('Database')) {
549
+
// For demonstration, let's replace a method with our own custom mock
0 commit comments