Skip to content

fix(jest-mock): restore withImplementation after errors#16219

Open
fallintoplace wants to merge 2 commits into
jestjs:mainfrom
fallintoplace:fix/withimplementation-restore-on-error
Open

fix(jest-mock): restore withImplementation after errors#16219
fallintoplace wants to merge 2 commits into
jestjs:mainfrom
fallintoplace:fix/withimplementation-restore-on-error

Conversation

@fallintoplace

Copy link
Copy Markdown

Summary

Fixes mockFn.withImplementation(...) so it restores the previous mock state when the callback throws synchronously or rejects asynchronously.

Closes #16218.

Motivation

withImplementation is documented as a temporary override, but the current implementation only restores state on the normal synchronous path and on fulfilled promises. That leaves the temporary implementation installed after exceptional exits.

How this works

  • restore the previous mock state in a shared helper
  • restore immediately when the callback throws
  • adopt thenables through the environment Promise and restore on both fulfillment and rejection
  • add regressions for sync throws and async rejections, including restoration of queued mockImplementationOnce state and whenCalledWith fallback behavior

Validation

  • yarn jest packages/jest-mock/src/__tests__/index.test.ts --runInBand
  • yarn jest packages/jest-mock/src/__tests__ --runInBand
  • yarn build:ts
  • yarn typecheck:tests

@netlify

netlify Bot commented May 25, 2026

Copy link
Copy Markdown

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 55ce323
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/6a14d180e163d400080c80d2
😎 Deploy Preview https://deploy-preview-16219--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.

@github-actions github-actions Bot added the require-changelog If a PR does requires a changelog entry label May 25, 2026
@fallintoplace fallintoplace marked this pull request as ready for review May 25, 2026 22:47
@pkg-pr-new

pkg-pr-new Bot commented May 25, 2026

Copy link
Copy Markdown

Open in StackBlitz

babel-jest

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

babel-plugin-jest-hoist

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

babel-preset-jest

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

create-jest

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

@jest/diff-sequences

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

expect

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

@jest/expect-utils

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

jest

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

jest-changed-files

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

jest-circus

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

jest-cli

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

jest-config

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

@jest/console

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

@jest/core

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

@jest/create-cache-key-function

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

jest-diff

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

jest-docblock

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

jest-each

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

@jest/environment

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

jest-environment-jsdom

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

@jest/environment-jsdom-abstract

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

jest-environment-node

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

@jest/expect

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

@jest/fake-timers

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

@jest/get-type

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

@jest/globals

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

jest-haste-map

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

jest-jasmine2

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

jest-leak-detector

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

jest-matcher-utils

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

jest-message-util

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

jest-mock

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

@jest/pattern

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

jest-phabricator

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

jest-regex-util

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

@jest/reporters

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

jest-resolve

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

jest-resolve-dependencies

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

jest-runner

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

jest-runtime

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

@jest/schemas

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

jest-snapshot

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

@jest/snapshot-utils

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

@jest/source-map

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

@jest/test-result

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

@jest/test-sequencer

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

@jest/transform

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

@jest/types

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

jest-util

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

jest-validate

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

jest-watcher

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

jest-worker

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

pretty-format

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

commit: 55ce323

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

require-changelog If a PR does requires a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: mockFn.withImplementation does not restore after throw or rejection

1 participant