Skip to content

fix: patch nock to schedule response delays on global timers - #9775

Open
ZayanKhan-12 wants to merge 1 commit into
MetaMask:mainfrom
ZayanKhan-12:fix/4428-nock-fake-timers
Open

fix: patch nock to schedule response delays on global timers#9775
ZayanKhan-12 wants to merge 1 commit into
MetaMask:mainfrom
ZayanKhan-12:fix/4428-nock-fake-timers

Conversation

@ZayanKhan-12

@ZayanKhan-12 ZayanKhan-12 commented Aug 4, 2026

Copy link
Copy Markdown

Explanation

nock v13 is not compatible with Jest fake timers: lib/common.js does const timers = require('timers') and captures timers.setTimeout into 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 the timers module, it patches the module's properties at useFakeTimers() time — after nock has already captured the original function. I verified this empirically on this repo (Jest 30.4.2, @sinonjs/fake-timers 15.4.0, nock 13.5.5): a test awaiting a nock response with a one-hour .delay() under fake timers times out despite jest.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 + a resolutions entry, following the existing @nktkas/hyperliquid precedent), with one deliberate refinement:

  • Positive response delays are scheduled on the global setTimeout, resolved at call time, so fake timers control them.
  • Zero-delay scheduling (nock routes every mocked response through common.setTimeout(respond, 0)) is left on the real timers module 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 timers module for response delays.

Testing

Verified with a scratch test (not committed, mirroring how the extension PR validated the patch) in @metamask/base-data-service, using jest.useFakeTimers({ doNotFake: ['nextTick', 'setImmediate'] }) per this repo's convention:

  1. A request against a nock interceptor with .delay(ONE_HOUR) resolves after jest.advanceTimersByTimeAsync(ONE_HOUR).
    • Before this patch: fails with "Exceeded timeout of 10000 ms" (delay runs on a real timer).
    • After this patch: passes in ~0.2s.
  2. A request against a no-delay interceptor resolves without advancing fake timers — passes both before and after, demonstrating no behavior change for the common case.

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 test
  • yarn constraints

Not run: the full monorepo test suite.

References

Fixes #4428

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
    • No changelog entries: this is a development-only change to a root dev dependency; no published package is affected.
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

🤖 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.5 so HTTP mock .delay() responses can be driven by Jest/Sinon fake timers instead of real time.

The patch replaces nock’s load-time timers.setTimeout wrapper with wrapDelayTimer: positive delays schedule on globalThis.setTimeout at call time; zero-delay responses still use the captured timers module timer so existing suites that use fake timers without advancing them keep getting immediate mocks.

Root package.json pins the patch via resolutions (same pattern as other patched deps); yarn.lock records 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.

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>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit cea9f24. Configure here.

+ }
+ }
+ const timerFn = timerArgs[0] > 0 ? globalThis.setTimeout : timer
+ const id = timerFn(cb, ...timerArgs)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit cea9f24. Configure here.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix nock compatibility with fake timers

1 participant