@@ -48,13 +48,19 @@ function abortIt(signal) {
48
48
return new AbortError ( undefined , { __proto__ : null , cause : signal . reason } ) ;
49
49
}
50
50
51
- const SUPPORTED_TIMERS = [ 'setTimeout' , 'setInterval' ] ;
51
+ const SUPPORTED_TIMERS = [ 'setTimeout' , 'setInterval' , 'setImmediate' ] ;
52
+ const TIMERS_DEFAULT_INTERVAL = {
53
+ __proto__ : null ,
54
+ setImmediate : - 1 ,
55
+ } ;
52
56
53
57
class MockTimers {
54
58
#realSetTimeout;
55
59
#realClearTimeout;
56
60
#realSetInterval;
57
61
#realClearInterval;
62
+ #realSetImmediate;
63
+ #realClearImmediate;
58
64
59
65
#realPromisifiedSetTimeout;
60
66
#realPromisifiedSetInterval;
@@ -63,6 +69,9 @@ class MockTimers {
63
69
#realTimersClearTimeout;
64
70
#realTimersSetInterval;
65
71
#realTimersClearInterval;
72
+ #realTimersSetImmediate;
73
+ #realTimersClearImmediate;
74
+ #realPromisifiedSetImmediate;
66
75
67
76
#timersInContext = [ ] ;
68
77
#isEnabled = false ;
@@ -76,6 +85,16 @@ class MockTimers {
76
85
#setInterval = FunctionPrototypeBind ( this . #createTimer, this , true ) ;
77
86
#clearInterval = FunctionPrototypeBind ( this . #clearTimer, this ) ;
78
87
88
+ #setImmediate = ( callback , ...args ) => {
89
+ return this . #createTimer(
90
+ false ,
91
+ callback ,
92
+ TIMERS_DEFAULT_INTERVAL . setImmediate ,
93
+ ...args ,
94
+ ) ;
95
+ } ;
96
+
97
+ #clearImmediate = FunctionPrototypeBind ( this . #clearTimer, this ) ;
79
98
constructor ( ) {
80
99
emitExperimentalWarning ( 'The MockTimers API' ) ;
81
100
}
@@ -158,7 +177,7 @@ class MockTimers {
158
177
yield * iterator ;
159
178
}
160
179
161
- #setTimeoutPromisified ( ms , result , options ) {
180
+ #promisifyTimer ( { timerFn , clearFn , ms, result, options } ) {
162
181
return new Promise ( ( resolve , reject ) => {
163
182
if ( options ?. signal ) {
164
183
try {
@@ -173,12 +192,12 @@ class MockTimers {
173
192
}
174
193
175
194
const onabort = ( ) => {
176
- this . #clearTimeout ( id ) ;
195
+ clearFn ( id ) ;
177
196
return reject ( abortIt ( options . signal ) ) ;
178
197
} ;
179
198
180
- const id = this . #setTimeout ( ( ) => {
181
- return resolve ( result || id ) ;
199
+ const id = timerFn ( ( ) => {
200
+ return resolve ( result ) ;
182
201
} , ms ) ;
183
202
184
203
if ( options ?. signal ) {
@@ -192,6 +211,28 @@ class MockTimers {
192
211
} ) ;
193
212
}
194
213
214
+ #setImmediatePromisified( result , options ) {
215
+ return this . #promisifyTimer( {
216
+ __proto__ : null ,
217
+ timerFn : FunctionPrototypeBind ( this . #setImmediate, this ) ,
218
+ clearFn : FunctionPrototypeBind ( this . #clearImmediate, this ) ,
219
+ ms : TIMERS_DEFAULT_INTERVAL . setImmediate ,
220
+ result,
221
+ options,
222
+ } ) ;
223
+ }
224
+
225
+ #setTimeoutPromisified( ms , result , options ) {
226
+ return this . #promisifyTimer( {
227
+ __proto__ : null ,
228
+ timerFn : FunctionPrototypeBind ( this . #setTimeout, this ) ,
229
+ clearFn : FunctionPrototypeBind ( this . #clearTimeout, this ) ,
230
+ ms,
231
+ result,
232
+ options,
233
+ } ) ;
234
+ }
235
+
195
236
#toggleEnableTimers( activate ) {
196
237
const options = {
197
238
__proto__ : null ,
@@ -233,6 +274,23 @@ class MockTimers {
233
274
this ,
234
275
) ;
235
276
} ,
277
+ setImmediate : ( ) => {
278
+ this . #realSetImmediate = globalThis . setImmediate ;
279
+ this . #realClearImmediate = globalThis . clearImmediate ;
280
+ this . #realTimersSetImmediate = nodeTimers . setImmediate ;
281
+ this . #realTimersClearImmediate = nodeTimers . clearImmediate ;
282
+
283
+ globalThis . setImmediate = this . #setImmediate;
284
+ globalThis . clearImmediate = this . #clearImmediate;
285
+
286
+ nodeTimers . setImmediate = this . #setImmediate;
287
+ nodeTimers . clearImmediate = this . #clearImmediate;
288
+
289
+ nodeTimersPromises . setImmediate = FunctionPrototypeBind (
290
+ this . #setImmediatePromisified,
291
+ this ,
292
+ ) ;
293
+ } ,
236
294
} ,
237
295
toReal : {
238
296
__proto__ : null ,
@@ -254,6 +312,15 @@ class MockTimers {
254
312
255
313
nodeTimersPromises . setInterval = this . #realPromisifiedSetInterval;
256
314
} ,
315
+ setImmediate : ( ) => {
316
+ globalThis . setImmediate = this . #realSetImmediate;
317
+ globalThis . clearImmediate = this . #realClearImmediate;
318
+
319
+ nodeTimers . setImmediate = this . #realTimersSetImmediate;
320
+ nodeTimers . clearImmediate = this . #realTimersClearImmediate;
321
+
322
+ nodeTimersPromises . setImmediate = this . #realPromisifiedSetImmediate;
323
+ } ,
257
324
} ,
258
325
} ;
259
326
0 commit comments