| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 48a1b93 commit 11fbd92
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1531,9 +1531,10 @@ added: | |||
| 1531 | 1531 | Enables timer mocking for the specified timers. | |
| 1532 | 1532 | ||
| 1533 | 1533 | * `timers` {Array} An optional array containing the timers to mock. | |
| 1534 | - The currently supported timer values are `'setInterval'` and `'setTimeout'`. | ||
| 1535 | - If no array is provided, all timers (`'setInterval'`, `'clearInterval'`, `'setTimeout'`, | ||
| 1536 | - and `'clearTimeout'`) will be mocked by default. | ||
| 1534 | + The currently supported timer values are `'setInterval'`, `'setTimeout'`, | ||
| 1535 | + and `'setImmediate'`. If no value is provided, all timers (`'setInterval'`, | ||
| 1536 | + `'clearInterval'`, `'setTimeout'`, `'clearTimeout'`, `'setImmediate'`, | ||
| 1537 | + and `'clearImmediate'`) will be mocked by default. | ||
| 1537 | 1538 | ||
| 1538 | 1539 | **Note:** When you enable mocking for a specific timer, its associated | |
| 1539 | 1540 | clear function will also be implicitly mocked. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -47,13 +47,19 @@ function abortIt(signal) { | |||
| 47 | 47 | return new AbortError(undefined, { __proto__: null, cause: signal.reason }); | |
| 48 | 48 | } | |
| 49 | 49 | ||
| 50 | - const SUPPORTED_TIMERS = ['setTimeout', 'setInterval']; | ||
| 50 | + const SUPPORTED_TIMERS = ['setTimeout', 'setInterval', 'setImmediate']; | ||
| 51 | + const TIMERS_DEFAULT_INTERVAL = { | ||
| 52 | + __proto__: null, | ||
| 53 | + setImmediate: -1, | ||
| 54 | + }; | ||
| 51 | 55 | ||
| 52 | 56 | class MockTimers { | |
| 53 | 57 | #realSetTimeout; | |
| 54 | 58 | #realClearTimeout; | |
| 55 | 59 | #realSetInterval; | |
| 56 | 60 | #realClearInterval; | |
| 61 | + #realSetImmediate; | ||
| 62 | + #realClearImmediate; | ||
| 57 | 63 | ||
| 58 | 64 | #realPromisifiedSetTimeout; | |
| 59 | 65 | #realPromisifiedSetInterval; | |
@@ -62,6 +68,9 @@ class MockTimers { | |||
| 62 | 68 | #realTimersClearTimeout; | |
| 63 | 69 | #realTimersSetInterval; | |
| 64 | 70 | #realTimersClearInterval; | |
| 71 | + #realTimersSetImmediate; | ||
| 72 | + #realTimersClearImmediate; | ||
| 73 | + #realPromisifiedSetImmediate; | ||
| 65 | 74 | ||
| 66 | 75 | #timersInContext = []; | |
| 67 | 76 | #isEnabled = false; | |
@@ -75,6 +84,16 @@ class MockTimers { | |||
| 75 | 84 | #setInterval = FunctionPrototypeBind(this.#createTimer, this, true); | |
| 76 | 85 | #clearInterval = FunctionPrototypeBind(this.#clearTimer, this); | |
| 77 | 86 | ||
| 87 | + #setImmediate = (callback, ...args) => { | ||
| 88 | + return this.#createTimer( | ||
| 89 | + false, | ||
| 90 | + callback, | ||
| 91 | + TIMERS_DEFAULT_INTERVAL.setImmediate, | ||
| 92 | + ...args, | ||
| 93 | + ); | ||
| 94 | + }; | ||
| 95 | + | ||
| 96 | + #clearImmediate = FunctionPrototypeBind(this.#clearTimer, this); | ||
| 78 | 97 | constructor() { | |
| 79 | 98 | emitExperimentalWarning('The MockTimers API'); | |
| 80 | 99 | } | |
@@ -157,7 +176,7 @@ class MockTimers { | |||
| 157 | 176 | yield* iterator; | |
| 158 | 177 | } | |
| 159 | 178 | ||
| 160 | - #setTimeoutPromisified(ms, result, options) { | ||
| 179 | + #promisifyTimer({ timerFn, clearFn, ms, result, options }) { | ||
| 161 | 180 | return new Promise((resolve, reject) => { | |
| 162 | 181 | if (options?.signal) { | |
| 163 | 182 | try { | |
@@ -172,12 +191,12 @@ class MockTimers { | |||
| 172 | 191 | } | |
| 173 | 192 | ||
| 174 | 193 | const onabort = () => { | |
| 175 | - this.#clearTimeout(id); | ||
| 194 | + clearFn(id); | ||
| 176 | 195 | return reject(abortIt(options.signal)); | |
| 177 | 196 | }; | |
| 178 | 197 | ||
| 179 | - const id = this.#setTimeout(() => { | ||
| 180 | - return resolve(result || id); | ||
| 198 | + const id = timerFn(() => { | ||
| 199 | + return resolve(result); | ||
| 181 | 200 | }, ms); | |
| 182 | 201 | ||
| 183 | 202 | if (options?.signal) { | |
@@ -191,6 +210,28 @@ class MockTimers { | |||
| 191 | 210 | }); | |
| 192 | 211 | } | |
| 193 | 212 | ||
| 213 | + #setImmediatePromisified(result, options) { | ||
| 214 | + return this.#promisifyTimer({ | ||
| 215 | + __proto__: null, | ||
| 216 | + timerFn: FunctionPrototypeBind(this.#setImmediate, this), | ||
| 217 | + clearFn: FunctionPrototypeBind(this.#clearImmediate, this), | ||
| 218 | + ms: TIMERS_DEFAULT_INTERVAL.setImmediate, | ||
| 219 | + result, | ||
| 220 | + options, | ||
| 221 | + }); | ||
| 222 | + } | ||
| 223 | + | ||
| 224 | + #setTimeoutPromisified(ms, result, options) { | ||
| 225 | + return this.#promisifyTimer({ | ||
| 226 | + __proto__: null, | ||
| 227 | + timerFn: FunctionPrototypeBind(this.#setTimeout, this), | ||
| 228 | + clearFn: FunctionPrototypeBind(this.#clearTimeout, this), | ||
| 229 | + ms, | ||
| 230 | + result, | ||
| 231 | + options, | ||
| 232 | + }); | ||
| 233 | + } | ||
| 234 | + | ||
| 194 | 235 | #toggleEnableTimers(activate) { | |
| 195 | 236 | const options = { | |
| 196 | 237 | __proto__: null, | |
@@ -232,6 +273,23 @@ class MockTimers { | |||
| 232 | 273 | this, | |
| 233 | 274 | ); | |
| 234 | 275 | }, | |
| 276 | + setImmediate: () => { | ||
| 277 | + this.#realSetImmediate = globalThis.setImmediate; | ||
| 278 | + this.#realClearImmediate = globalThis.clearImmediate; | ||
| 279 | + this.#realTimersSetImmediate = nodeTimers.setImmediate; | ||
| 280 | + this.#realTimersClearImmediate = nodeTimers.clearImmediate; | ||
| 281 | + | ||
| 282 | + globalThis.setImmediate = this.#setImmediate; | ||
| 283 | + globalThis.clearImmediate = this.#clearImmediate; | ||
| 284 | + | ||
| 285 | + nodeTimers.setImmediate = this.#setImmediate; | ||
| 286 | + nodeTimers.clearImmediate = this.#clearImmediate; | ||
| 287 | + | ||
| 288 | + nodeTimersPromises.setImmediate = FunctionPrototypeBind( | ||
| 289 | + this.#setImmediatePromisified, | ||
| 290 | + this, | ||
| 291 | + ); | ||
| 292 | + }, | ||
| 235 | 293 | }, | |
| 236 | 294 | toReal: { | |
| 237 | 295 | __proto__: null, | |
@@ -253,6 +311,15 @@ class MockTimers { | |||
| 253 | 311 | ||
| 254 | 312 | nodeTimersPromises.setInterval = this.#realPromisifiedSetInterval; | |
| 255 | 313 | }, | |
| 314 | + setImmediate: () => { | ||
| 315 | + globalThis.setImmediate = this.#realSetImmediate; | ||
| 316 | + globalThis.clearImmediate = this.#realClearImmediate; | ||
| 317 | + | ||
| 318 | + nodeTimers.setImmediate = this.#realTimersSetImmediate; | ||
| 319 | + nodeTimers.clearImmediate = this.#realTimersClearImmediate; | ||
| 320 | + | ||
| 321 | + nodeTimersPromises.setImmediate = this.#realPromisifiedSetImmediate; | ||
| 322 | + }, | ||
| 256 | 323 | }, | |
| 257 | 324 | }; | |
| 258 | 325 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments