Skip to content

Conversation

@ide
Copy link
Contributor

@ide ide commented Aug 2, 2025

Summary

getMockImplementation() was made a public method in commit 89119e4 in 2016 and in public TypeScript types since commit 95b94e8 in 2020 but it was never documented. Closes #9735.

Motivation

This method has become more necessary with Jest's support for ESM. Previously, you could extend manual mocks using jest.spyOn():

// __mocks__/module.js
export function fn() { /* default mock implementation */ }
// __tests__/module.test.js
const originalFn = module.fn;
jest.spyOn(module, 'fn').mockImplementation((...args) => {
  // Add custom logic for the test here
  return originalFn(...args);
});

However, spyOn doesn't work with ESM because module object properties are read-only. The workaround is to use getMockImplementation():

// __mocks__/module.js
export const fn = jest.fn(() => { /* default mock implementation */ });
// __tests__/module.test.js
const originalImpl = jest.mocked(module.fn).getMockImplementation();
jest.mocked(module.fn).mockImplementation((...args) => {
  // Add custom logic for the test here
  return originalImpl?.(...args);
});

Test plan

  1. Build and serve the docs locally: cd website && yarn install && yarn fetchSupporters && yarn start
  2. Browse to http://localhost:3000/docs/next/mock-function-api#mockfngetmockimplementation

`getMockImplementation()` was made a public method in commit 89119e4 in 2016 and in public TypeScript types since commit 95b94e8 in 2020 but it was never documented.

Motivation
---
This method has become more necessary with Jest's support for ESM. Previously, you could
extend manual mocks using jest.spyOn():

```js title="__mocks__/module.js"
export function fn() { /* default mock implementation */ }
```

```js title="__tests__/module.test.js"
const originalFn = module.fn;
jest.spyOn(module, 'fn').mockImplementation((...args) => {
  // Add custom logic for the test here
  return originalFn(...args);
});
```

However, spyOn doesn't work with ESM because module object properties are read-only.
The workaround is to use getMockImplementation():

```js title="__mocks__/module.js"
export const fn = jest.fn(() => { /* default mock implementation */ });
```

```js title="__tests__/module.test.js"
const originalImpl = jest.mocked(module.fn).getMockImplementation();
jest.mocked(module.fn).mockImplementation((...args) => {
  // Add custom logic for the test here
  return originalImpl?.(...args);
});
```

Test Plan
---
1. Build and serve the docs locally: `cd website && yarn install && yarn fetchSupporters && yarn start`
2. Browse to http://localhost:3000/docs/next/mock-function-api#mockfngetmockimplementation
@netlify
Copy link

netlify bot commented Aug 2, 2025

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 37267a5
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/688e38ee0a6f910008a4b01e
😎 Deploy Preview https://deploy-preview-15766--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Aug 2, 2025

Open in StackBlitz

babel-jest

npm i https://pkg.pr.new/babel-jest@15766

babel-plugin-jest-hoist

npm i https://pkg.pr.new/babel-plugin-jest-hoist@15766

babel-preset-jest

npm i https://pkg.pr.new/babel-preset-jest@15766

create-jest

npm i https://pkg.pr.new/create-jest@15766

@jest/diff-sequences

npm i https://pkg.pr.new/@jest/diff-sequences@15766

expect

npm i https://pkg.pr.new/expect@15766

@jest/expect-utils

npm i https://pkg.pr.new/@jest/expect-utils@15766

jest

npm i https://pkg.pr.new/jest@15766

jest-changed-files

npm i https://pkg.pr.new/jest-changed-files@15766

jest-circus

npm i https://pkg.pr.new/jest-circus@15766

jest-cli

npm i https://pkg.pr.new/jest-cli@15766

jest-config

npm i https://pkg.pr.new/jest-config@15766

@jest/console

npm i https://pkg.pr.new/@jest/console@15766

@jest/core

npm i https://pkg.pr.new/@jest/core@15766

@jest/create-cache-key-function

npm i https://pkg.pr.new/@jest/create-cache-key-function@15766

jest-diff

npm i https://pkg.pr.new/jest-diff@15766

jest-docblock

npm i https://pkg.pr.new/jest-docblock@15766

jest-each

npm i https://pkg.pr.new/jest-each@15766

@jest/environment

npm i https://pkg.pr.new/@jest/environment@15766

jest-environment-jsdom

npm i https://pkg.pr.new/jest-environment-jsdom@15766

@jest/environment-jsdom-abstract

npm i https://pkg.pr.new/@jest/environment-jsdom-abstract@15766

jest-environment-node

npm i https://pkg.pr.new/jest-environment-node@15766

@jest/expect

npm i https://pkg.pr.new/@jest/expect@15766

@jest/fake-timers

npm i https://pkg.pr.new/@jest/fake-timers@15766

@jest/get-type

npm i https://pkg.pr.new/@jest/get-type@15766

@jest/globals

npm i https://pkg.pr.new/@jest/globals@15766

jest-haste-map

npm i https://pkg.pr.new/jest-haste-map@15766

jest-jasmine2

npm i https://pkg.pr.new/jest-jasmine2@15766

jest-leak-detector

npm i https://pkg.pr.new/jest-leak-detector@15766

jest-matcher-utils

npm i https://pkg.pr.new/jest-matcher-utils@15766

jest-message-util

npm i https://pkg.pr.new/jest-message-util@15766

jest-mock

npm i https://pkg.pr.new/jest-mock@15766

@jest/pattern

npm i https://pkg.pr.new/@jest/pattern@15766

jest-phabricator

npm i https://pkg.pr.new/jest-phabricator@15766

jest-regex-util

npm i https://pkg.pr.new/jest-regex-util@15766

@jest/reporters

npm i https://pkg.pr.new/@jest/reporters@15766

jest-resolve

npm i https://pkg.pr.new/jest-resolve@15766

jest-resolve-dependencies

npm i https://pkg.pr.new/jest-resolve-dependencies@15766

jest-runner

npm i https://pkg.pr.new/jest-runner@15766

jest-runtime

npm i https://pkg.pr.new/jest-runtime@15766

@jest/schemas

npm i https://pkg.pr.new/@jest/schemas@15766

jest-snapshot

npm i https://pkg.pr.new/jest-snapshot@15766

@jest/snapshot-utils

npm i https://pkg.pr.new/@jest/snapshot-utils@15766

@jest/source-map

npm i https://pkg.pr.new/@jest/source-map@15766

@jest/test-result

npm i https://pkg.pr.new/@jest/test-result@15766

@jest/test-sequencer

npm i https://pkg.pr.new/@jest/test-sequencer@15766

@jest/transform

npm i https://pkg.pr.new/@jest/transform@15766

@jest/types

npm i https://pkg.pr.new/@jest/types@15766

jest-util

npm i https://pkg.pr.new/jest-util@15766

jest-validate

npm i https://pkg.pr.new/jest-validate@15766

jest-watcher

npm i https://pkg.pr.new/jest-watcher@15766

jest-worker

npm i https://pkg.pr.new/jest-worker@15766

pretty-format

npm i https://pkg.pr.new/pretty-format@15766

commit: 37267a5

Copy link
Member

@cpojer cpojer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@cpojer cpojer merged commit a787c53 into jestjs:main Aug 27, 2025
74 checks passed
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 27, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

missing docs for getMockImplementation of the mock api

2 participants