-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Fix
nock
compatibility with fake timers
The `nock` library is not compatible with the fake timers we use in unit tests because it uses the Node.js `timers` API. This API is not mocked correctly by the version of Jest we are using. Jest uses `@sinon/fake-timers` internally, which didn't support mocking the Node.js `timers` API until v11.0.0 (see sinonjs/fake-timers#467) This package is updated in Jest as part of the v30 release, which is currently under development. To workaround this problem in the meantime, the `nock` package has been updated and patched to use global timers rather than the `timers` API. Global timers are mocked correctly. This was required for some unit tests that I will be submitting in a different PR, intended for #24503
- Loading branch information
Showing
3 changed files
with
62 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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,45 @@ | ||
diff --git a/lib/common.js b/lib/common.js | ||
index 336bc4d376d07306d6adf79b8e73cffd4dfff4f7..271d50d42a5a370440fd93de00072ddfdd2fcf4b 100644 | ||
--- a/lib/common.js | ||
+++ b/lib/common.js | ||
@@ -1,7 +1,17 @@ | ||
'use strict' | ||
|
||
const debug = require('debug')('nock.common') | ||
-const timers = require('timers') | ||
+/** | ||
+ * PATCH NOTES: | ||
+ * | ||
+ * Replace Node.js `timers` module with global timers, because Jest/Sinon fake timers do not work | ||
+ * with the Node.js timers module in the version we are using. | ||
+ * | ||
+ * This is resolved in `@sinon/fake-timers@11`, which is introduced in the upcoming `jest@30` | ||
+ * release. | ||
+ * | ||
+ * TODO: Remove this patch after updating to `jest@30`. | ||
+ */ | ||
const url = require('url') | ||
const util = require('util') | ||
|
||
@@ -598,16 +608,16 @@ const intervals = [] | ||
const immediates = [] | ||
|
||
const wrapTimer = | ||
- (timer, ids) => | ||
+ (timerName, ids) => | ||
(...args) => { | ||
- const id = timer(...args) | ||
+ const id = globalThis[timerName](...args) | ||
ids.push(id) | ||
return id | ||
} | ||
|
||
-const setTimeout = wrapTimer(timers.setTimeout, timeouts) | ||
-const setInterval = wrapTimer(timers.setInterval, intervals) | ||
-const setImmediate = wrapTimer(timers.setImmediate, immediates) | ||
+const setTimeout = wrapTimer('setTimeout', timeouts) | ||
+const setInterval = wrapTimer('setInterval', intervals) | ||
+const setImmediate = wrapTimer('setImmediate', immediates) | ||
|
||
function clearTimer(clear, ids) { | ||
while (ids.length) { |
This file contains 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 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