fix: patch nock to schedule response delays on global timers - #9775
Open
ZayanKhan-12 wants to merge 1 commit into
Open
fix: patch nock to schedule response delays on global timers#9775ZayanKhan-12 wants to merge 1 commit into
ZayanKhan-12 wants to merge 1 commit into
Conversation
nock v13 captures setTimeout from the Node timers module at load time, which Jest/Sinon fake timers cannot mock, so delayed nock responses always ran in real time and could not be advanced with fake timers. Patch nock via the Yarn patch protocol to resolve the global setTimeout at call time for positive response delays, so they are controlled by fake timers. Zero-delay scheduling is left on the real timers module function so that existing tests which enable fake timers without advancing them keep receiving undelayed mock responses. Fixes MetaMask#4428 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Reviewed by Cursor Bugbot for commit cea9f24. Configure here.
| + } | ||
| + } | ||
| + const timerFn = timerArgs[0] > 0 ? globalThis.setTimeout : timer | ||
| + const id = timerFn(cb, ...timerArgs) |
There was a problem hiding this comment.
Delay patch breaks fake-timer tests
High Severity
Routing positive nock delays through globalThis.setTimeout makes .delay() fake-timer-controlled, but several existing suites still await those delayed mocks without advancing timers afterward. Those tests previously relied on real wall-clock delays and will hang or fail under this patch, contrary to the claim that no expectations change.
Reviewed by Cursor Bugbot for commit cea9f24. Configure here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Explanation
nockv13 is not compatible with Jest fake timers:lib/common.jsdoesconst timers = require('timers')and capturestimers.setTimeoutinto its timer wrappers at module load time. Jest/Sinon fake timers replace the global timer functions, but they cannot affect the function reference nock captured at load time, so.delay()-ed nock responses always run in real time and cannot be advanced with fake timers.Notably, upgrading to Jest 30 did not fix this, contrary to the expectation recorded in #4428 and in the extension's patch notes. Even though
@sinonjs/fake-timers≥ 11 can fake thetimersmodule, it patches the module's properties atuseFakeTimers()time — after nock has already captured the original function. I verified this empirically on this repo (Jest 30.4.2,@sinonjs/fake-timers15.4.0, nock 13.5.5): a test awaiting a nock response with a one-hour.delay()under fake timers times out despitejest.advanceTimersByTimeAsync, because the delay is scheduled on a real timer.This PR applies the equivalent of the extension's patch (MetaMask/metamask-extension#24805) via the Yarn patch protocol (
.yarn/patches+ aresolutionsentry, following the existing@nktkas/hyperliquidprecedent), with one deliberate refinement:setTimeout, resolved at call time, so fake timers control them.common.setTimeout(respond, 0)) is left on the realtimersmodule function.The refinement matters because applying the extension patch verbatim breaks existing suites in this repo: 21 test files across ~14 packages use nock together with fake timers, and many await a mocked response before advancing timers. With the verbatim patch, every one of those responses would require explicit timer advancement — e.g.
@metamask/base-data-service's suite fails with test timeouts. With this refinement, only actual.delay()s become fake-timer-controlled, which is the behavior the issue asks for, and no existing test's expectations change.The patch can be removed when nock is updated to v14, which no longer uses the
timersmodule for response delays.Testing
Verified with a scratch test (not committed, mirroring how the extension PR validated the patch) in
@metamask/base-data-service, usingjest.useFakeTimers({ doNotFake: ['nextTick', 'setImmediate'] })per this repo's convention:.delay(ONE_HOUR)resolves afterjest.advanceTimersByTimeAsync(ONE_HOUR).Also ran (all passing with the patch applied):
yarn workspace @metamask/base-data-service run test(fails with test timeouts under the verbatim extension patch; passes with this one)yarn workspace @metamask/ramps-controller run testyarn constraintsNot run: the full monorepo test suite.
References
Fixes #4428
Checklist
🤖 Generated with Claude Code
Note
Low Risk
Dev-only dependency patch affecting test HTTP mocking; no production runtime or published package behavior changes.
Overview
Adds a Yarn patch for
nock@13.5.5so HTTP mock.delay()responses can be driven by Jest/Sinon fake timers instead of real time.The patch replaces nock’s load-time
timers.setTimeoutwrapper withwrapDelayTimer: positive delays schedule onglobalThis.setTimeoutat call time; zero-delay responses still use the capturedtimersmodule timer so existing suites that use fake timers without advancing them keep getting immediate mocks.Root
package.jsonpins the patch viaresolutions(same pattern as other patched deps);yarn.lockrecords the patched package entry. Intended to be removed when upgrading to nock v14.Reviewed by Cursor Bugbot for commit cea9f24. Bugbot is set up for automated code reviews on this repo. Configure here.