forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-runner-mock-timers.js
831 lines (691 loc) · 28.5 KB
/
test-runner-mock-timers.js
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
'use strict';
process.env.NODE_TEST_KNOWN_GLOBALS = 0;
const common = require('../common');
const assert = require('node:assert');
const { it, mock, describe } = require('node:test');
const nodeTimers = require('node:timers');
const nodeTimersPromises = require('node:timers/promises');
describe('Mock Timers Test Suite', () => {
describe('MockTimers API', () => {
it('should throw an error if trying to enable a timer that is not supported', (t) => {
assert.throws(() => {
t.mock.timers.enable({ apis: ['DOES_NOT_EXIST'] });
}, {
code: 'ERR_INVALID_ARG_VALUE',
});
});
it('should throw an error if trying to enable a timer twice', (t) => {
t.mock.timers.enable();
assert.throws(() => {
t.mock.timers.enable();
}, {
code: 'ERR_INVALID_STATE',
});
});
it('should not throw if calling reset without enabling timers', (t) => {
t.mock.timers.reset();
});
it('should throw an error if calling tick without enabling timers', (t) => {
assert.throws(() => {
t.mock.timers.tick();
}, {
code: 'ERR_INVALID_STATE',
});
});
it('should throw an error if calling tick with a negative number', (t) => {
t.mock.timers.enable();
assert.throws(() => {
t.mock.timers.tick(-1);
}, {
code: 'ERR_INVALID_ARG_VALUE',
});
});
it('should check that propertyDescriptor gets back after resetting timers', (t) => {
const getDescriptor = (ctx, fn) => Object.getOwnPropertyDescriptor(ctx, fn);
const getCurrentTimersDescriptors = () => {
const timers = [
'setTimeout',
'clearTimeout',
'setInterval',
'clearInterval',
'setImmediate',
'clearImmediate',
];
const globalTimersDescriptors = timers.map((fn) => getDescriptor(global, fn));
const nodeTimersDescriptors = timers.map((fn) => getDescriptor(nodeTimers, fn));
const nodeTimersPromisesDescriptors = timers
.filter((fn) => !fn.includes('clear'))
.map((fn) => getDescriptor(nodeTimersPromises, fn));
return {
global: globalTimersDescriptors,
nodeTimers: nodeTimersDescriptors,
nodeTimersPromises: nodeTimersPromisesDescriptors,
};
};
const originalDescriptors = getCurrentTimersDescriptors();
t.mock.timers.enable();
const during = getCurrentTimersDescriptors();
t.mock.timers.reset();
const after = getCurrentTimersDescriptors();
for (const env in originalDescriptors) {
for (const prop in originalDescriptors[env]) {
const originalDescriptor = originalDescriptors[env][prop];
const afterDescriptor = after[env][prop];
assert.deepStrictEqual(
originalDescriptor,
afterDescriptor,
);
assert.notDeepStrictEqual(
originalDescriptor,
during[env][prop],
);
assert.notDeepStrictEqual(
during[env][prop],
after[env][prop],
);
}
}
});
it('should reset all timers when calling .reset function', (t) => {
t.mock.timers.enable();
const fn = t.mock.fn();
global.setTimeout(fn, 1000);
t.mock.timers.reset();
assert.deepStrictEqual(Date.now, globalThis.Date.now);
assert.throws(() => {
t.mock.timers.tick(1000);
}, {
code: 'ERR_INVALID_STATE',
});
assert.strictEqual(fn.mock.callCount(), 0);
});
it('should reset all timers when calling Symbol.dispose', (t) => {
t.mock.timers.enable();
const fn = t.mock.fn();
global.setTimeout(fn, 1000);
// TODO(benjamingr) refactor to `using`
t.mock.timers[Symbol.dispose]();
assert.throws(() => {
t.mock.timers.tick(1000);
}, {
code: 'ERR_INVALID_STATE',
});
assert.strictEqual(fn.mock.callCount(), 0);
});
it('should execute in order if timeout is the same', (t) => {
t.mock.timers.enable();
const order = [];
const fn1 = t.mock.fn(() => order.push('f1'));
const fn2 = t.mock.fn(() => order.push('f2'));
global.setTimeout(fn1, 1000);
global.setTimeout(fn2, 1000);
t.mock.timers.tick(1000);
assert.strictEqual(fn1.mock.callCount(), 1);
assert.strictEqual(fn2.mock.callCount(), 1);
assert.deepStrictEqual(order, ['f1', 'f2']);
});
describe('runAll Suite', () => {
it('should throw an error if calling runAll without enabling timers', (t) => {
assert.throws(() => {
t.mock.timers.runAll();
}, {
code: 'ERR_INVALID_STATE',
});
});
it('should trigger all timers when calling .runAll function', async (t) => {
const timeoutFn = t.mock.fn();
const intervalFn = t.mock.fn();
t.mock.timers.enable();
global.setTimeout(timeoutFn, 1111);
const id = global.setInterval(intervalFn, 9999);
t.mock.timers.runAll();
global.clearInterval(id);
assert.strictEqual(timeoutFn.mock.callCount(), 1);
assert.strictEqual(intervalFn.mock.callCount(), 1);
});
it('should increase the epoch as the tick run for runAll', async (t) => {
const timeoutFn = t.mock.fn();
const intervalFn = t.mock.fn();
t.mock.timers.enable();
global.setTimeout(timeoutFn, 1111);
const id = global.setInterval(intervalFn, 9999);
t.mock.timers.runAll();
global.clearInterval(id);
assert.strictEqual(timeoutFn.mock.callCount(), 1);
assert.strictEqual(intervalFn.mock.callCount(), 1);
assert.strictEqual(Date.now(), 9999);
});
it('should not error if there are not timers to run', (t) => {
t.mock.timers.enable();
t.mock.timers.runAll();
// Should not throw
});
});
});
describe('globals/timers', () => {
describe('setTimeout Suite', () => {
it('should advance in time and trigger timers when calling the .tick function', (t) => {
mock.timers.enable({ apis: ['setTimeout'] });
const fn = mock.fn();
global.setTimeout(fn, 4000);
mock.timers.tick(4000);
assert.strictEqual(fn.mock.callCount(), 1);
mock.timers.reset();
});
it('should advance in time and trigger timers when calling the .tick function multiple times', (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const fn = t.mock.fn();
global.setTimeout(fn, 2000);
t.mock.timers.tick(1000);
assert.strictEqual(fn.mock.callCount(), 0);
t.mock.timers.tick(500);
assert.strictEqual(fn.mock.callCount(), 0);
t.mock.timers.tick(500);
assert.strictEqual(fn.mock.callCount(), 1);
});
it('should work with the same params as the original setTimeout', (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const fn = t.mock.fn();
const args = ['a', 'b', 'c'];
global.setTimeout(fn, 2000, ...args);
t.mock.timers.tick(1000);
t.mock.timers.tick(500);
t.mock.timers.tick(500);
assert.strictEqual(fn.mock.callCount(), 1);
assert.deepStrictEqual(fn.mock.calls[0].arguments, args);
});
it('should keep setTimeout working if timers are disabled', (t, done) => {
const now = Date.now();
const timeout = 2;
const expected = () => now - timeout;
global.setTimeout(common.mustCall(() => {
assert.strictEqual(now - timeout, expected());
done();
}), timeout);
});
});
describe('clearTimeout Suite', () => {
it('should not advance in time if clearTimeout was invoked', (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const fn = mock.fn();
const id = global.setTimeout(fn, 4000);
global.clearTimeout(id);
t.mock.timers.tick(4000);
assert.strictEqual(fn.mock.callCount(), 0);
});
});
describe('setInterval Suite', () => {
it('should tick three times using fake setInterval', (t) => {
t.mock.timers.enable({ apis: ['setInterval'] });
const fn = t.mock.fn();
const id = global.setInterval(fn, 200);
t.mock.timers.tick(200);
t.mock.timers.tick(200);
t.mock.timers.tick(200);
global.clearInterval(id);
assert.strictEqual(fn.mock.callCount(), 3);
});
it('should work with the same params as the original setInterval', (t) => {
t.mock.timers.enable({ apis: ['setInterval'] });
const fn = t.mock.fn();
const args = ['a', 'b', 'c'];
const id = global.setInterval(fn, 200, ...args);
t.mock.timers.tick(200);
t.mock.timers.tick(200);
t.mock.timers.tick(200);
global.clearInterval(id);
assert.strictEqual(fn.mock.callCount(), 3);
assert.deepStrictEqual(fn.mock.calls[0].arguments, args);
assert.deepStrictEqual(fn.mock.calls[1].arguments, args);
assert.deepStrictEqual(fn.mock.calls[2].arguments, args);
});
});
describe('clearInterval Suite', () => {
it('should not advance in time if clearInterval was invoked', (t) => {
t.mock.timers.enable({ apis: ['setInterval'] });
const fn = mock.fn();
const id = global.setInterval(fn, 200);
global.clearInterval(id);
t.mock.timers.tick(200);
assert.strictEqual(fn.mock.callCount(), 0);
});
});
describe('setImmediate Suite', () => {
it('should keep setImmediate working if timers are disabled', (t, done) => {
const now = Date.now();
const timeout = 2;
const expected = () => now - timeout;
global.setImmediate(common.mustCall(() => {
assert.strictEqual(now - timeout, expected());
done();
}));
});
it('should work with the same params as the original setImmediate', (t) => {
t.mock.timers.enable({ apis: ['setImmediate'] });
const fn = t.mock.fn();
const args = ['a', 'b', 'c'];
global.setImmediate(fn, ...args);
t.mock.timers.tick(9999);
assert.strictEqual(fn.mock.callCount(), 1);
assert.deepStrictEqual(fn.mock.calls[0].arguments, args);
});
it('should not advance in time if clearImmediate was invoked', (t) => {
t.mock.timers.enable({ apis: ['setImmediate'] });
const id = global.setImmediate(common.mustNotCall());
global.clearImmediate(id);
t.mock.timers.tick(200);
});
it('should advance in time and trigger timers when calling the .tick function', (t) => {
t.mock.timers.enable({ apis: ['setImmediate'] });
global.setImmediate(common.mustCall(1));
t.mock.timers.tick(0);
});
it('should execute in order if setImmediate is called multiple times', (t) => {
t.mock.timers.enable({ apis: ['setImmediate'] });
const order = [];
const fn1 = t.mock.fn(common.mustCall(() => order.push('f1'), 1));
const fn2 = t.mock.fn(common.mustCall(() => order.push('f2'), 1));
global.setImmediate(fn1);
global.setImmediate(fn2);
t.mock.timers.tick(0);
assert.deepStrictEqual(order, ['f1', 'f2']);
});
it('should execute setImmediate first if setTimeout was also called', (t) => {
t.mock.timers.enable({ apis: ['setImmediate', 'setTimeout'] });
const order = [];
const fn1 = t.mock.fn(common.mustCall(() => order.push('f1'), 1));
const fn2 = t.mock.fn(common.mustCall(() => order.push('f2'), 1));
global.setTimeout(fn2, 0);
global.setImmediate(fn1);
t.mock.timers.tick(100);
assert.deepStrictEqual(order, ['f1', 'f2']);
});
});
describe('timers/promises', () => {
describe('setTimeout Suite', () => {
it('should advance in time and trigger timers when calling the .tick function multiple times', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const p = nodeTimersPromises.setTimeout(2000);
t.mock.timers.tick(1000);
t.mock.timers.tick(500);
t.mock.timers.tick(500);
t.mock.timers.tick(500);
p.then(common.mustCall((result) => {
assert.strictEqual(result, undefined);
}));
});
it('should work with the same params as the original timers/promises/setTimeout', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const expectedResult = 'result';
const controller = new AbortController();
const p = nodeTimersPromises.setTimeout(2000, expectedResult, {
ref: true,
signal: controller.signal,
});
t.mock.timers.tick(1000);
t.mock.timers.tick(500);
t.mock.timers.tick(500);
t.mock.timers.tick(500);
const result = await p;
assert.strictEqual(result, expectedResult);
});
it('should always return the same result as the original timers/promises/setTimeout', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
for (const expectedResult of [undefined, null, false, true, 0, 0n, 1, 1n, '', 'result', {}]) {
const p = nodeTimersPromises.setTimeout(2000, expectedResult);
t.mock.timers.tick(2000);
const result = await p;
assert.strictEqual(result, expectedResult);
}
});
it('should abort operation if timers/promises/setTimeout received an aborted signal', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const expectedResult = 'result';
const controller = new AbortController();
const p = nodeTimersPromises.setTimeout(2000, expectedResult, {
ref: true,
signal: controller.signal,
});
t.mock.timers.tick(1000);
controller.abort();
t.mock.timers.tick(500);
t.mock.timers.tick(500);
t.mock.timers.tick(500);
await assert.rejects(() => p, {
name: 'AbortError',
});
});
it('should abort operation even if the .tick was not called', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const expectedResult = 'result';
const controller = new AbortController();
const p = nodeTimersPromises.setTimeout(2000, expectedResult, {
ref: true,
signal: controller.signal,
});
controller.abort();
await assert.rejects(() => p, {
name: 'AbortError',
});
});
it('should abort operation when .abort is called before calling setInterval', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const expectedResult = 'result';
const controller = new AbortController();
controller.abort();
const p = nodeTimersPromises.setTimeout(2000, expectedResult, {
ref: true,
signal: controller.signal,
});
await assert.rejects(() => p, {
name: 'AbortError',
});
});
it('should reject given an an invalid signal instance', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const expectedResult = 'result';
const p = nodeTimersPromises.setTimeout(2000, expectedResult, {
ref: true,
signal: {},
});
await assert.rejects(() => p, {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
});
});
// Test for https://github.com/nodejs/node/issues/50365
it('should not affect other timers when aborting', async (t) => {
const f1 = t.mock.fn();
const f2 = t.mock.fn();
t.mock.timers.enable({ apis: ['setTimeout'] });
const ac = new AbortController();
// id 1 & pos 1 in priority queue
nodeTimersPromises.setTimeout(100, undefined, { signal: ac.signal }).then(f1, f1);
// id 2 & pos 1 in priority queue (id 1 is moved to pos 2)
nodeTimersPromises.setTimeout(50).then(f2, f2);
ac.abort(); // BUG: will remove timer at pos 1 not timer with id 1!
t.mock.timers.runAll();
await nodeTimersPromises.setImmediate(); // let promises settle
// First setTimeout is aborted
assert.strictEqual(f1.mock.callCount(), 1);
assert.strictEqual(f1.mock.calls[0].arguments[0].code, 'ABORT_ERR');
// Second setTimeout should resolve, but never settles, because it was eronously removed by ac.abort()
assert.strictEqual(f2.mock.callCount(), 1);
});
// Test for https://github.com/nodejs/node/issues/50365
it('should not affect other timers when aborted after triggering', async (t) => {
const f1 = t.mock.fn();
const f2 = t.mock.fn();
t.mock.timers.enable({ apis: ['setTimeout'] });
const ac = new AbortController();
// id 1 & pos 1 in priority queue
nodeTimersPromises.setTimeout(50, true, { signal: ac.signal }).then(f1, f1);
// id 2 & pos 2 in priority queue
nodeTimersPromises.setTimeout(100).then(f2, f2);
// First setTimeout resolves
t.mock.timers.tick(50);
await nodeTimersPromises.setImmediate(); // let promises settle
assert.strictEqual(f1.mock.callCount(), 1);
assert.strictEqual(f1.mock.calls[0].arguments.length, 1);
assert.strictEqual(f1.mock.calls[0].arguments[0], true);
// Now timer with id 2 will be at pos 1 in priority queue
ac.abort(); // BUG: will remove timer at pos 1 not timer with id 1!
// Second setTimeout should resolve, but never settles, because it was eronously removed by ac.abort()
t.mock.timers.runAll();
await nodeTimersPromises.setImmediate(); // let promises settle
assert.strictEqual(f2.mock.callCount(), 1);
});
});
describe('setInterval Suite', () => {
it('should tick three times using fake setInterval', async (t) => {
t.mock.timers.enable({ apis: ['setInterval'] });
const interval = 100;
const intervalIterator = nodeTimersPromises.setInterval(interval, Date.now());
const first = intervalIterator.next();
const second = intervalIterator.next();
const third = intervalIterator.next();
t.mock.timers.tick(interval);
t.mock.timers.tick(interval);
t.mock.timers.tick(interval);
t.mock.timers.tick(interval);
const results = await Promise.all([
first,
second,
third,
]);
const finished = await intervalIterator.return();
assert.deepStrictEqual(finished, { done: true, value: undefined });
results.forEach((result) => {
assert.strictEqual(typeof result.value, 'number');
assert.strictEqual(result.done, false);
});
});
it('should tick five times testing a real use case', async (t) => {
t.mock.timers.enable({ apis: ['setInterval'] });
const expectedIterations = 5;
const interval = 1000;
let time = 0;
async function run() {
const times = [];
for await (const _ of nodeTimersPromises.setInterval(interval)) { // eslint-disable-line no-unused-vars
time += interval;
times.push(time);
if (times.length === expectedIterations) break;
}
return times;
}
const r = run();
t.mock.timers.tick(interval);
t.mock.timers.tick(interval);
t.mock.timers.tick(interval);
t.mock.timers.tick(interval);
t.mock.timers.tick(interval);
const timeResults = await r;
assert.strictEqual(timeResults.length, expectedIterations);
for (let it = 1; it < expectedIterations; it++) {
assert.strictEqual(timeResults[it - 1], interval * it);
}
});
it('should always return the same result as the original timers/promises/setInterval', async (t) => {
t.mock.timers.enable({ apis: ['setInterval'] });
for (const expectedResult of [undefined, null, false, true, 0, 0n, 1, 1n, '', 'result', {}]) {
const intervalIterator = nodeTimersPromises.setInterval(2000, expectedResult);
const p = intervalIterator.next();
t.mock.timers.tick(2000);
const result = await p;
await intervalIterator.return();
assert.strictEqual(result.done, false);
assert.strictEqual(result.value, expectedResult);
}
});
it('should abort operation given an abort controller signal', async (t) => {
t.mock.timers.enable({ apis: ['setInterval'] });
const interval = 100;
const abortController = new AbortController();
const intervalIterator = nodeTimersPromises.setInterval(interval, Date.now(), {
signal: abortController.signal,
});
const first = intervalIterator.next();
const second = intervalIterator.next();
t.mock.timers.tick(interval);
abortController.abort();
t.mock.timers.tick(interval);
const firstResult = await first;
// Interval * 2 because value can be a little bit greater than interval
assert.ok(firstResult.value < Date.now() + interval * 2);
assert.strictEqual(firstResult.done, false);
await assert.rejects(() => second, {
name: 'AbortError',
});
});
it('should abort operation when .abort is called before calling setInterval', async (t) => {
t.mock.timers.enable({ apis: ['setInterval'] });
const interval = 100;
const abortController = new AbortController();
abortController.abort();
const intervalIterator = nodeTimersPromises.setInterval(interval, Date.now(), {
signal: abortController.signal,
});
const first = intervalIterator.next();
t.mock.timers.tick(interval);
await assert.rejects(() => first, {
name: 'AbortError',
});
});
it('should abort operation given an abort controller signal on a real use case', async (t) => {
t.mock.timers.enable({ apis: ['setInterval'] });
const controller = new AbortController();
const signal = controller.signal;
const interval = 200;
const expectedIterations = 2;
let numIterations = 0;
async function run() {
const it = nodeTimersPromises.setInterval(interval, undefined, { signal });
for await (const _ of it) { // eslint-disable-line no-unused-vars
numIterations += 1;
if (numIterations === 5) break;
}
}
const r = run();
t.mock.timers.tick(interval);
t.mock.timers.tick(interval);
controller.abort();
t.mock.timers.tick(interval);
t.mock.timers.tick(interval);
t.mock.timers.tick(interval);
t.mock.timers.tick(interval);
await assert.rejects(() => r, {
name: 'AbortError',
});
assert.strictEqual(numIterations, expectedIterations);
});
// Test for https://github.com/nodejs/node/issues/50381
it('should use the correct interval', (t) => {
t.mock.timers.enable({ apis: ['setInterval'] });
const fn = t.mock.fn();
setInterval(fn, 1000);
assert.strictEqual(fn.mock.callCount(), 0);
t.mock.timers.tick(1000);
assert.strictEqual(fn.mock.callCount(), 1);
t.mock.timers.tick(1);
t.mock.timers.tick(1);
t.mock.timers.tick(1);
assert.strictEqual(fn.mock.callCount(), 1);
});
// Test for https://github.com/nodejs/node/issues/50382
it('should not prevent due timers to be processed', async (t) => {
t.mock.timers.enable({ apis: ['setInterval', 'setTimeout'] });
const f1 = t.mock.fn();
const f2 = t.mock.fn();
setInterval(f1, 1000);
setTimeout(f2, 1001);
assert.strictEqual(f1.mock.callCount(), 0);
assert.strictEqual(f2.mock.callCount(), 0);
t.mock.timers.tick(1001);
assert.strictEqual(f1.mock.callCount(), 1);
assert.strictEqual(f2.mock.callCount(), 1);
});
});
});
});
describe('Date Suite', () => {
it('should return the initial UNIX epoch if not specified', (t) => {
t.mock.timers.enable({ apis: ['Date'] });
const date = new Date();
assert.strictEqual(date.getTime(), 0);
assert.strictEqual(Date.now(), 0);
});
it('should throw an error if setTime is called without enabling timers', (t) => {
assert.throws(
() => {
t.mock.timers.setTime(100);
},
{ code: 'ERR_INVALID_STATE' }
);
});
it('should throw an error if epoch passed to enable is not valid', (t) => {
assert.throws(
() => {
t.mock.timers.enable({ now: -1 });
},
{ code: 'ERR_INVALID_ARG_VALUE' }
);
assert.throws(
() => {
t.mock.timers.enable({ now: 'string' });
},
{ code: 'ERR_INVALID_ARG_TYPE' }
);
assert.throws(
() => {
t.mock.timers.enable({ now: NaN });
},
{ code: 'ERR_INVALID_ARG_VALUE' }
);
});
it('should replace the original Date with the mocked one', (t) => {
t.mock.timers.enable({ apis: ['Date'] });
assert.ok(Date.isMock);
});
it('should return the ticked time when calling Date.now after tick', (t) => {
t.mock.timers.enable({ apis: ['Date'] });
const time = 100;
t.mock.timers.tick(time);
assert.strictEqual(Date.now(), time);
});
it('should return the Date as string when calling it as a function', (t) => {
t.mock.timers.enable({ apis: ['Date'] });
const returned = Date();
// Matches the format: 'Mon Jan 01 1970 00:00:00'
// We don't care about the date, just the format
assert.ok(/\w{3}\s\w{3}\s\d{1,2}\s\d{2,4}\s\d{1,2}:\d{2}:\d{2}/.test(returned));
});
it('should return the date with different argument calls', (t) => {
t.mock.timers.enable({ apis: ['Date'] });
assert.strictEqual(new Date(0).getTime(), 0);
assert.strictEqual(new Date(100).getTime(), 100);
assert.strictEqual(new Date('1970-01-01T00:00:00.000Z').getTime(), 0);
assert.strictEqual(new Date(1970, 0).getFullYear(), 1970);
assert.strictEqual(new Date(1970, 0).getMonth(), 0);
assert.strictEqual(new Date(1970, 0, 1).getDate(), 1);
assert.strictEqual(new Date(1970, 0, 1, 11).getHours(), 11);
assert.strictEqual(new Date(1970, 0, 1, 11, 10).getMinutes(), 10);
assert.strictEqual(new Date(1970, 0, 1, 11, 10, 45).getSeconds(), 45);
assert.strictEqual(new Date(1970, 0, 1, 11, 10, 45, 898).getMilliseconds(), 898);
assert.strictEqual(new Date(1970, 0, 1, 11, 10, 45, 898).toDateString(), 'Thu Jan 01 1970');
});
it('should return native code when calling Date.toString', (t) => {
t.mock.timers.enable({ apis: ['Date'] });
assert.strictEqual(Date.toString(), 'function Date() { [native code] }');
});
it('should start with a custom epoch if the second argument is specified', (t) => {
t.mock.timers.enable({ apis: ['Date'], now: 100 });
const date1 = new Date();
assert.strictEqual(date1.getTime(), 100);
t.mock.timers.reset();
t.mock.timers.enable({ apis: ['Date'], now: new Date(200) });
const date2 = new Date();
assert.strictEqual(date2.getTime(), 200);
});
it('should replace epoch if setTime is lesser than now and not tick', (t) => {
t.mock.timers.enable();
const fn = t.mock.fn();
const id = setTimeout(fn, 1000);
t.mock.timers.setTime(800);
assert.strictEqual(Date.now(), 800);
t.mock.timers.setTime(500);
assert.strictEqual(Date.now(), 500);
assert.strictEqual(fn.mock.callCount(), 0);
clearTimeout(id);
});
it('should not tick time when setTime is called', (t) => {
t.mock.timers.enable();
const fn = t.mock.fn();
const id = setTimeout(fn, 1000);
t.mock.timers.setTime(1200);
assert.strictEqual(Date.now(), 1200);
assert.strictEqual(fn.mock.callCount(), 0);
clearTimeout(id);
});
});
});