-
-
Notifications
You must be signed in to change notification settings - Fork 293
fix: patch nock to schedule response delays on global timers #9775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ZayanKhan-12
wants to merge
1
commit into
MetaMask:main
Choose a base branch
from
ZayanKhan-12:fix/4428-nock-fake-timers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+64
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) { | ||
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
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
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.
There was a problem hiding this comment.
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.setTimeoutmakes.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.