ITADN

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

#16218Openfallintoplace 创建于 2026-05-25
F
fallintoplacecommented
## Version 30.4.2 ## Steps to reproduce Minimal reproduction repo: https://github.com/fallintoplace/jest-withimplementation-restore-repro 1. Clone the reproduction repo. 2. Run `npm install`. 3. Run `npm test`. ## Expected behavior `mockFn.withImplementation(...)` should always restore the previous mock state after the callback finishes, including when the callback throws synchronously or rejects asynchronously. In the reproduction: - the sync-throw case should restore the queued `mockImplementationOnce(() => 'once')` - the async-rejection case should restore the previous default implementation ## Actual behavior The temporary implementation leaks after exceptional exits: - after a sync throw, the mock still returns the temporary implementation instead of the queued `mockImplementationOnce` - after an async rejection, the mock still returns the temporary implementation instead of the original default implementation ## Additional context The public docs describe `withImplementation` as a temporary override and say that awaiting the async form resets the implementation. The current behavior only restores on the normal synchronous path and on fulfilled promises. ## Environment ```shell System: OS: macOS 26.5 CPU: (8) arm64 Apple M1 Pro Binaries: Node: 25.8.2 - /opt/homebrew/bin/node npm: 11.11.1 - /opt/homebrew/bin/npm npmPackages: jest: 30.4.2 => 30.4.2 ```
0 条评论