deno.land / std@0.224.0 / testing / time_test.ts

time_test.ts
View Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { assert, assertEquals, assertInstanceOf, assertNotEquals, assertRejects, assertStrictEquals,} from "../assert/mod.ts";import { FakeTime, TimeError } from "./time.ts";import { _internals } from "./_time.ts";import { assertSpyCall, spy, type SpyCall } from "./mock.ts";
function fromNow(): () => number { const start: number = Date.now(); return () => Date.now() - start;}
Deno.test("FakeTime doesn't affect Date unchanged if uninitialized", () => { assertStrictEquals(Date, _internals.Date);});
Deno.test("FakeTime fakes Date", () => { { using _time = new FakeTime(9001); assertNotEquals(Date, _internals.Date); } assertStrictEquals(Date, _internals.Date);});
Deno.test("FakeTime causes date parse and UTC behave the same", () => { const expectedUTC = Date.UTC(96, 1, 2, 3, 4, 5); const expectedParse = Date.parse("04 Dec 1995 00:12:00 GMT");
using _time = new FakeTime(); assertEquals( Date.UTC(96, 1, 2, 3, 4, 5), expectedUTC, ); assertEquals( Date.parse("04 Dec 1995 00:12:00 GMT"), expectedParse, );});
Deno.test("FakeTime causes Date.now() returns current fake time", () => { const time: FakeTime = new FakeTime(9001); const now = spy(_internals.Date, "now"); try { try { assertEquals(Date.now(), 9001); assertEquals(now.calls.length, 0); time.tick(1523); assertEquals(Date.now(), 10524); assertEquals(now.calls.length, 0); } finally { time.restore(); } assertNotEquals(Date.now(), 10524); assertEquals(now.calls.length, 1); } finally { now.restore(); }});
Deno.test("FakeTime causes Date instance methods passthrough to real Date instance methods", () => { using _time = new FakeTime(); const now = new Date("2020-05-25T05:00:00.12345Z"); assertEquals(now.toISOString(), "2020-05-25T05:00:00.123Z");
const func1 = spy( _internals.Date.prototype, "toISOString", ); try { now.toISOString(); assertSpyCall(func1, 0, { args: [], returned: "2020-05-25T05:00:00.123Z", }); assertInstanceOf(func1.calls?.[0]?.self, _internals.Date); } finally { func1.restore(); }
const func2 = spy( _internals.Date.prototype, Symbol.toPrimitive, ); try { Number(now); assertSpyCall(func2, 0, { args: ["number"], returned: 1590382800123, }); } finally { func2.restore(); }});
Deno.test("FakeTime timeout functions unchanged if FakeTime is uninitialized", () => { assertStrictEquals(setTimeout, _internals.setTimeout); assertStrictEquals(clearTimeout, _internals.clearTimeout);});
Deno.test("FakeTime timeout functions are fake if FakeTime is initialized", () => { { using _time: FakeTime = new FakeTime(); assertNotEquals(setTimeout, _internals.setTimeout); assertNotEquals(clearTimeout, _internals.clearTimeout); } assertStrictEquals(setTimeout, _internals.setTimeout); assertStrictEquals(clearTimeout, _internals.clearTimeout);});
Deno.test("FakeTime only ticks forward when setting now or calling tick", () => { using time: FakeTime = new FakeTime(); const start: number = Date.now();
assertEquals(Date.now(), start); time.tick(5); assertEquals(Date.now(), start + 5); time.now = start + 1000; assertEquals(Date.now(), start + 1000); assert(_internals.Date.now() < start + 1000);});
Deno.test("FakeTime controls timeouts", () => { using time: FakeTime = new FakeTime(); const start: number = Date.now(); const cb = spy(fromNow()); const expected: SpyCall[] = [];
setTimeout(cb, 1000); time.tick(250); assertEquals(cb.calls, expected); time.tick(250); assertEquals(cb.calls, expected); time.tick(500); expected.push({ args: [], returned: 1000 }); assertEquals(cb.calls, expected); time.tick(2500); assertEquals(cb.calls, expected);
setTimeout(cb, 1000, "a"); setTimeout(cb, 2000, "b"); setTimeout(cb, 1500, "c"); assertEquals(cb.calls, expected); time.tick(2500); expected.push({ args: ["a"], returned: 4500 }); expected.push({ args: ["c"], returned: 5000 }); expected.push({ args: ["b"], returned: 5500 }); assertEquals(cb.calls, expected);
setTimeout(cb, 1000, "a"); setTimeout(cb, 1500, "b"); const timeout: number = setTimeout(cb, 1750, "c"); setTimeout(cb, 2000, "d"); time.tick(1250); expected.push({ args: ["a"], returned: 7000 }); assertEquals(cb.calls, expected); assertEquals(Date.now(), start + 7250); clearTimeout(timeout); time.tick(500); expected.push({ args: ["b"], returned: 7500 }); assertEquals(cb.calls, expected); assertEquals(Date.now(), start + 7750); time.tick(250); expected.push({ args: ["d"], returned: 8000 }); assertEquals(cb.calls, expected);});
Deno.test("FakeTime interval functions unchanged if FakeTime is uninitialized", () => { assertStrictEquals(setInterval, _internals.setInterval); assertStrictEquals(clearInterval, _internals.clearInterval);});
Deno.test("FakeTime fakes interval functions", () => { { using _time: FakeTime = new FakeTime(); assertNotEquals(setInterval, _internals.setInterval); assertNotEquals(clearInterval, _internals.clearInterval); } assertStrictEquals(setInterval, _internals.setInterval); assertStrictEquals(clearInterval, _internals.clearInterval);});
Deno.test("FakeTime controls intervals", () => { using time: FakeTime = new FakeTime(); const cb = spy(fromNow()); const expected: SpyCall[] = [];
const interval: number = setInterval(cb, 1000); time.tick(250); assertEquals(cb.calls, expected); time.tick(250); assertEquals(cb.calls, expected); time.tick(500); expected.push({ args: [], returned: 1000 }); assertEquals(cb.calls, expected); time.tick(2500); expected.push({ args: [], returned: 2000 }); expected.push({ args: [], returned: 3000 }); assertEquals(cb.calls, expected);
clearInterval(interval); time.tick(1000); assertEquals(cb.calls, expected);});
Deno.test("FakeTime calls timeout and interval callbacks in correct order", () => { using time: FakeTime = new FakeTime(); const cb = spy(fromNow()); const timeoutCb = spy(cb); const intervalCb = spy(cb); const expected: SpyCall[] = []; const timeoutExpected: SpyCall[] = []; const intervalExpected: SpyCall[] = [];
const interval: number = setInterval(intervalCb, 1000); setTimeout(timeoutCb, 500); time.tick(250); assertEquals(intervalCb.calls, intervalExpected); time.tick(250); setTimeout(timeoutCb, 1000); let expect: SpyCall = { args: [], returned: 500 }; expected.push(expect); timeoutExpected.push(expect); assertEquals(cb.calls, expected); assertEquals(timeoutCb.calls, timeoutExpected); assertEquals(cb.calls, expected); assertEquals(intervalCb.calls, intervalExpected); time.tick(500); expect = { args: [], returned: 1000 }; expected.push(expect); intervalExpected.push(expect); assertEquals(cb.calls, expected); assertEquals(intervalCb.calls, intervalExpected); time.tick(2500); expect = { args: [], returned: 1500 }; expected.push(expect); timeoutExpected.push(expect); expect = { args: [], returned: 2000 }; expected.push(expect); intervalExpected.push(expect); expect = { args: [], returned: 3000 }; expected.push(expect); intervalExpected.push(expect); assertEquals(cb.calls, expected); assertEquals(timeoutCb.calls, timeoutExpected); assertEquals(intervalCb.calls, intervalExpected);
clearInterval(interval); time.tick(1000); assertEquals(cb.calls, expected); assertEquals(timeoutCb.calls, timeoutExpected); assertEquals(intervalCb.calls, intervalExpected);});
Deno.test("FakeTime.restoreFor() restores real time temporarily", async () => { using time: FakeTime = new FakeTime(); const start: number = Date.now();
assertEquals(Date.now(), start); time.tick(1000); assertEquals(Date.now(), start + 1000); assert(_internals.Date.now() < start + 1000); await FakeTime.restoreFor(() => { assert(Date.now() < start + 1000); }); assertEquals(Date.now(), start + 1000); assert(_internals.Date.now() < start + 1000);});
Deno.test("FakeTime.restoreFor() restores real time and re-overridden atomically", async () => { using _time: FakeTime = new FakeTime(); const fakeSetTimeout = setTimeout; const actualSetTimeouts: (typeof setTimeout)[] = [];
const asyncFn = async () => { actualSetTimeouts.push(setTimeout); await Promise.resolve(); actualSetTimeouts.push(setTimeout); await Promise.resolve(); actualSetTimeouts.push(setTimeout); }; const promise = asyncFn(); await new Promise((resolve) => { FakeTime.restoreFor(() => setTimeout(resolve, 0)); }); await promise; assertEquals(actualSetTimeouts, [ fakeSetTimeout, fakeSetTimeout, fakeSetTimeout, ]);});
Deno.test("FakeTime.restoreFor() returns promise that resolved to result of callback", async () => { using _time: FakeTime = new FakeTime();
const resultSync = await FakeTime.restoreFor(() => "a"); assertEquals(resultSync, "a"); const resultAsync = await FakeTime.restoreFor(() => Promise.resolve("b")); assertEquals(resultAsync, "b");});
Deno.test("FakeTime.restoreFor() returns promise that rejected to error in callback", async () => { using _time: FakeTime = new FakeTime();
await assertRejects( () => FakeTime.restoreFor(() => { throw new Error("Error in sync callback"); }), Error, "Error in sync callback", ); await assertRejects( () => FakeTime.restoreFor(() => { return Promise.reject(new Error("Error in async callback")); }), Error, "Error in async callback", );});
Deno.test("FakeTime.restoreFor() returns promise that rejected to TimeError if FakeTime is uninitialized", async () => { await assertRejects( () => FakeTime.restoreFor(() => {}), TimeError, "no fake time", );});
Deno.test("FakeTime.delay() uses real time", async () => { using time: FakeTime = new FakeTime(); const start: number = Date.now();
assertEquals(Date.now(), start); await time.delay(20); assert(_internals.Date.now() >= start + 20); assertEquals(Date.now(), start);});
Deno.test("FakeTime.delay() runs all microtasks before resolving", async () => { using time: FakeTime = new FakeTime();
const seq = []; queueMicrotask(() => seq.push(2)); queueMicrotask(() => seq.push(3)); seq.push(1); await time.delay(20); seq.push(4); assertEquals(seq, [1, 2, 3, 4]);});
Deno.test("FakeTime.delay() works with abort", async () => { using time: FakeTime = new FakeTime();
const seq = []; const abort = new AbortController(); const { signal } = abort; const delayedPromise = time.delay(100, { signal }); seq.push(1); await FakeTime.restoreFor(() => { setTimeout(() => { seq.push(2); abort.abort(); }, 0); }); await assertRejects( () => delayedPromise, DOMException, "Delay was aborted", ); seq.push(3); assertEquals(seq, [1, 2, 3]);});
Deno.test("FakeTime.runMicrotasks() runs all microtasks before resolving", async () => { using time: FakeTime = new FakeTime(); const start: number = Date.now();
const seq = []; queueMicrotask(() => seq.push(2)); queueMicrotask(() => seq.push(3)); seq.push(1); await time.runMicrotasks(); seq.push(4); assertEquals(seq, [1, 2, 3, 4]); assertEquals(Date.now(), start);});
Deno.test("FakeTime.tickAsync() runs all microtasks and runs timers if ticks past due", async () => { using time: FakeTime = new FakeTime(); const start: number = Date.now(); const cb = spy(fromNow()); const expected: SpyCall[] = []; const seq: number[] = [];
setTimeout(cb, 1000); queueMicrotask(() => seq.push(2)); queueMicrotask(() => seq.push(3)); seq.push(1); await time.tickAsync(250); seq.push(4); assertEquals(cb.calls, expected); await time.tickAsync(250); assertEquals(cb.calls, expected); queueMicrotask(() => seq.push(6)); seq.push(5); await time.tickAsync(500); seq.push(7); expected.push({ args: [], returned: 1000 }); assertEquals(cb.calls, expected); assertEquals(Date.now(), start + 1000); assertEquals(seq, [1, 2, 3, 4, 5, 6, 7]);});
Deno.test("FakeTime.next() runs next timer without running microtasks", async () => { using time: FakeTime = new FakeTime(); const start: number = Date.now(); const cb = spy(fromNow()); const seq: number[] = [];
// Callback is called by `next`. setTimeout(cb, 1000); queueMicrotask(() => seq.push(3)); queueMicrotask(() => seq.push(4)); seq.push(1); const hasNextTimer = time.next(); seq.push(2);
assertEquals(hasNextTimer, true); const expectedCalls = [{ args: [] as [], returned: 1000 }]; assertEquals(cb.calls, expectedCalls); assertEquals(Date.now(), start + 1000); await time.runMicrotasks();
// Callback is already called before `next` called. queueMicrotask(() => seq.push(7)); queueMicrotask(() => seq.push(8)); seq.push(5); const hasNextTimerAfterCalled = time.next(); seq.push(6); await time.runMicrotasks();
assertEquals(hasNextTimerAfterCalled, false); assertEquals(cb.calls, expectedCalls); assertEquals(Date.now(), start + 1000);
// Callbacks are cleared before `next` called. const id1 = setTimeout(cb, 1000); const id2 = setTimeout(cb, 2000); clearTimeout(id1); clearTimeout(id2); queueMicrotask(() => seq.push(11)); queueMicrotask(() => seq.push(12)); seq.push(9); const hasNextTimerAfterCleared = time.next(); seq.push(10); await time.runMicrotasks();
assertEquals(hasNextTimerAfterCleared, false); assertEquals(cb.calls, expectedCalls); assertEquals(Date.now(), start + 1000);
// Callback is partially cleared before `next` called. const id3 = setTimeout(cb, 1500); setTimeout(cb, 1500); clearTimeout(id3); queueMicrotask(() => seq.push(15)); queueMicrotask(() => seq.push(16)); seq.push(13); const hasNextTimerAfterPartiallyCleared = time.next(); seq.push(14); await time.runMicrotasks();
assertEquals(hasNextTimerAfterPartiallyCleared, true); const expectedCalls2 = [ { args: [] as [], returned: 1000 }, { args: [] as [], returned: 1000 + 1500 }, ]; assertEquals(cb.calls, expectedCalls2); assertEquals(Date.now(), start + 1000 + 1500);
assertEquals(seq, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);});
Deno.test("FakeTime.nextAsync() runs all microtasks and next timer", async () => { using time: FakeTime = new FakeTime(); const start: number = Date.now(); const cb = spy(fromNow()); const seq: number[] = [];
// Callback is called by `nextAsync`. setTimeout(cb, 1000); queueMicrotask(() => seq.push(2)); queueMicrotask(() => seq.push(3)); seq.push(1); const hasNextTimer = await time.nextAsync(); seq.push(4);
assertEquals(hasNextTimer, true); const expectedCalls = [{ args: [] as [], returned: 1000 }]; assertEquals(cb.calls, expectedCalls); assertEquals(Date.now(), start + 1000);
// Callback is already called before `nextAsync` called. queueMicrotask(() => seq.push(6)); queueMicrotask(() => seq.push(7)); seq.push(5); const hasNextTimerAfterCalled = await time.nextAsync(); seq.push(8);
assertEquals(hasNextTimerAfterCalled, false); assertEquals(cb.calls, expectedCalls); assertEquals(Date.now(), start + 1000);
// Callbacks are cleared before `nextAsync` called. const id1 = setTimeout(cb, 1000); const id2 = setTimeout(cb, 2000); clearTimeout(id1); clearTimeout(id2); queueMicrotask(() => seq.push(10)); queueMicrotask(() => seq.push(11)); seq.push(9); const hasNextTimerAfterCleared = await time.nextAsync(); seq.push(12);
assertEquals(hasNextTimerAfterCleared, false); assertEquals(cb.calls, expectedCalls); assertEquals(Date.now(), start + 1000);
// Callback is partially cleared before `nextAsync` called. const id3 = setTimeout(cb, 1500); setTimeout(cb, 1500); clearTimeout(id3); queueMicrotask(() => seq.push(14)); queueMicrotask(() => seq.push(15)); seq.push(13); const hasNextTimerAfterPartiallyCleared = await time.nextAsync(); seq.push(16); await time.runMicrotasks();
assertEquals(hasNextTimerAfterPartiallyCleared, true); const expectedCalls2 = [ { args: [] as [], returned: 1000 }, { args: [] as [], returned: 1000 + 1500 }, ]; assertEquals(cb.calls, expectedCalls2); assertEquals(Date.now(), start + 1000 + 1500);
assertEquals(seq, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);});
Deno.test("FakeTime.runAll() runs all timers without running microtasks", async () => { using time: FakeTime = new FakeTime(); const start: number = Date.now(); const cb = spy(fromNow()); const seq: number[] = [];
setTimeout(cb, 1000); setTimeout(cb, 1500); queueMicrotask(() => seq.push(3)); queueMicrotask(() => seq.push(4)); seq.push(1); time.runAll(); seq.push(2); const expectedCalls = [ { args: [] as [], returned: 1000 }, { args: [] as [], returned: 1500 }, ]; assertEquals(cb.calls, expectedCalls); assertEquals(Date.now(), start + 1500); await time.runMicrotasks();
queueMicrotask(() => seq.push(7)); queueMicrotask(() => seq.push(8)); seq.push(5); time.runAll(); seq.push(6); await time.runMicrotasks();
assertEquals(cb.calls, expectedCalls); assertEquals(Date.now(), start + 1500); assertEquals(seq, [1, 2, 3, 4, 5, 6, 7, 8]);});
Deno.test("FakeTime.runAllAsync() runs all microtasks and timers", async () => { using time: FakeTime = new FakeTime(); const start: number = Date.now(); const cb = spy(fromNow()); const seq: number[] = [];
setTimeout(cb, 1000); setTimeout(cb, 1500); queueMicrotask(() => seq.push(2)); queueMicrotask(() => seq.push(3)); seq.push(1); await time.runAllAsync(); seq.push(4); const expectedCalls = [ { args: [] as [], returned: 1000 }, { args: [] as [], returned: 1500 }, ]; assertEquals(cb.calls, expectedCalls); assertEquals(Date.now(), start + 1500);
queueMicrotask(() => seq.push(6)); queueMicrotask(() => seq.push(7)); seq.push(5); await time.runAllAsync(); seq.push(8);
assertEquals(cb.calls, expectedCalls); assertEquals(Date.now(), start + 1500); assertEquals(seq, [1, 2, 3, 4, 5, 6, 7, 8]);});
const Date_ = Date;
Deno.test("Date from FakeTime is structured cloneable", () => { using _time: FakeTime = new FakeTime(); const date: Date = new Date(); assert(date instanceof Date); assert(date instanceof Date_); const cloned: Date = structuredClone(date); assertEquals(cloned.getTime(), date.getTime()); assert(date instanceof Date); assert(cloned instanceof Date_);});
std

Version Info

Tagged at
6 months ago