From cea9f24e45745826b2614c5794917898d9e73e8c Mon Sep 17 00:00:00 2001 From: ZayanKhan-12 <108294002+ZayanKhan-12@users.noreply.github.com> Date: Mon, 3 Aug 2026 21:22:11 -0400 Subject: [PATCH] fix: patch nock to schedule response delays on global timers 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 #4428 Co-Authored-By: Claude Fable 5 --- .../patches/nock-npm-13.5.5-ccb57f0a2f.patch | 51 +++++++++++++++++++ package.json | 1 + yarn.lock | 13 ++++- 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 .yarn/patches/nock-npm-13.5.5-ccb57f0a2f.patch diff --git a/.yarn/patches/nock-npm-13.5.5-ccb57f0a2f.patch b/.yarn/patches/nock-npm-13.5.5-ccb57f0a2f.patch new file mode 100644 index 00000000000..f9b83cc1d35 --- /dev/null +++ b/.yarn/patches/nock-npm-13.5.5-ccb57f0a2f.patch @@ -0,0 +1,51 @@ +diff --git a/lib/common.js b/lib/common.js +index a6f7f766d03fb7acc9fe2494488685684fa7fe07..1d29b572e18ee94c4a76e593e69cf628f9e18d69 100644 +--- a/lib/common.js ++++ b/lib/common.js +@@ -612,7 +612,45 @@ const wrapTimer = + return id + } + +-const setTimeout = wrapTimer(timers.setTimeout, timeouts) ++/** ++ * PATCH NOTES (https://github.com/MetaMask/core/issues/4428): ++ * ++ * Schedule positive response delays (e.g. from `.delay()`) on the global ++ * `setTimeout`, resolved at call time, instead of the `timers` module ++ * function captured at module load time. Jest/Sinon fake timers replace the ++ * global timer functions but cannot affect the load-time-captured `timers` ++ * module reference, so delayed responses were not controllable by fake ++ * timers and ran in real time. ++ * ++ * Zero-delay scheduling (used for every mocked response) is deliberately ++ * left on the real `timers` module function so that tests which enable fake ++ * timers but do not advance them still receive undelayed mock responses, as ++ * they do today. ++ * ++ * This is a more conservative version of the patch applied in the extension ++ * repository: https://github.com/MetaMask/metamask-extension/pull/24805 ++ * ++ * TODO: Remove this patch after updating to `nock@14`, which no longer uses ++ * the `timers` module for response delays. ++ */ ++const wrapDelayTimer = ++ (timer, ids) => ++ (callback, ...timerArgs) => { ++ const cb = (...callbackArgs) => { ++ try { ++ // eslint-disable-next-line n/no-callback-literal ++ callback(...callbackArgs) ++ } finally { ++ ids.delete(id) ++ } ++ } ++ const timerFn = timerArgs[0] > 0 ? globalThis.setTimeout : timer ++ const id = timerFn(cb, ...timerArgs) ++ ids.add(id) ++ return id ++ } ++ ++const setTimeout = wrapDelayTimer(timers.setTimeout, timeouts) + const setImmediate = wrapTimer(timers.setImmediate, immediates) + + function clearTimer(clear, ids) { diff --git a/package.json b/package.json index 486e49e6aa1..3cbe8da4246 100644 --- a/package.json +++ b/package.json @@ -119,6 +119,7 @@ "@nktkas/hyperliquid@npm:^0.33.1": "patch:@nktkas/hyperliquid@npm%3A0.33.1#~/.yarn/patches/@nktkas-hyperliquid-npm-0.33.1-6a541fdd1d.patch", "elliptic@6.5.4": "^6.5.7", "fast-xml-parser@^4.3.4": "^4.4.1", + "nock@npm:^13.3.1": "patch:nock@npm%3A13.5.5#~/.yarn/patches/nock-npm-13.5.5-ccb57f0a2f.patch", "ws@7.4.6": "^7.5.10" }, "simple-git-hooks": { diff --git a/yarn.lock b/yarn.lock index 1cdbe464c28..16fb765ffe5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -22106,7 +22106,7 @@ __metadata: languageName: node linkType: hard -"nock@npm:^13.3.1": +"nock@npm:13.5.5": version: 13.5.5 resolution: "nock@npm:13.5.5" dependencies: @@ -22117,6 +22117,17 @@ __metadata: languageName: node linkType: hard +"nock@patch:nock@npm%3A13.5.5#~/.yarn/patches/nock-npm-13.5.5-ccb57f0a2f.patch": + version: 13.5.5 + resolution: "nock@patch:nock@npm%3A13.5.5#~/.yarn/patches/nock-npm-13.5.5-ccb57f0a2f.patch::version=13.5.5&hash=5834ac" + dependencies: + debug: "npm:^4.1.0" + json-stringify-safe: "npm:^5.0.1" + propagate: "npm:^2.0.0" + checksum: 10/4e8a6eeac8f3997ade78d65234d2489e33aeab144977fc129cf567b5a9010ea959cc32989d923d366f8836adead4e320495d0624a57a93013f993d277a3a4ee1 + languageName: node + linkType: hard + "node-abi@npm:^3.3.0": version: 3.92.0 resolution: "node-abi@npm:3.92.0"