Skip to content

Commit 0f8b826

Browse files
badkeyymarco-ippolito
authored andcommitted
test: split up test-runner-mock-timers test
PR-URL: #55506 Reviewed-By: Erick Wendel <erick.workspace@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Claudio Wunder <cwunder@gnome.org>
1 parent 046430c commit 0f8b826

3 files changed

+248
-236
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
'use strict';
2+
process.env.NODE_TEST_KNOWN_GLOBALS = 0;
3+
require('../common');
4+
5+
const assert = require('node:assert');
6+
const { it, describe } = require('node:test');
7+
8+
describe('Mock Timers Date Test Suite', () => {
9+
it('should return the initial UNIX epoch if not specified', (t) => {
10+
t.mock.timers.enable({ apis: ['Date'] });
11+
const date = new Date();
12+
assert.strictEqual(date.getTime(), 0);
13+
assert.strictEqual(Date.now(), 0);
14+
});
15+
16+
it('should throw an error if setTime is called without enabling timers', (t) => {
17+
assert.throws(
18+
() => {
19+
t.mock.timers.setTime(100);
20+
},
21+
{ code: 'ERR_INVALID_STATE' }
22+
);
23+
});
24+
25+
it('should throw an error if epoch passed to enable is not valid', (t) => {
26+
assert.throws(
27+
() => {
28+
t.mock.timers.enable({ now: -1 });
29+
},
30+
{ code: 'ERR_INVALID_ARG_VALUE' }
31+
);
32+
33+
assert.throws(
34+
() => {
35+
t.mock.timers.enable({ now: 'string' });
36+
},
37+
{ code: 'ERR_INVALID_ARG_TYPE' }
38+
);
39+
40+
assert.throws(
41+
() => {
42+
t.mock.timers.enable({ now: NaN });
43+
},
44+
{ code: 'ERR_INVALID_ARG_VALUE' }
45+
);
46+
});
47+
48+
it('should replace the original Date with the mocked one', (t) => {
49+
t.mock.timers.enable({ apis: ['Date'] });
50+
assert.ok(Date.isMock);
51+
});
52+
53+
it('should return the ticked time when calling Date.now after tick', (t) => {
54+
t.mock.timers.enable({ apis: ['Date'] });
55+
const time = 100;
56+
t.mock.timers.tick(time);
57+
assert.strictEqual(Date.now(), time);
58+
});
59+
60+
it('should return the Date as string when calling it as a function', (t) => {
61+
t.mock.timers.enable({ apis: ['Date'] });
62+
const returned = Date();
63+
// Matches the format: 'Mon Jan 01 1970 00:00:00'
64+
// We don't care about the date, just the format
65+
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));
66+
});
67+
68+
it('should return the date with different argument calls', (t) => {
69+
t.mock.timers.enable({ apis: ['Date'] });
70+
assert.strictEqual(new Date(0).getTime(), 0);
71+
assert.strictEqual(new Date(100).getTime(), 100);
72+
assert.strictEqual(new Date('1970-01-01T00:00:00.000Z').getTime(), 0);
73+
assert.strictEqual(new Date(1970, 0).getFullYear(), 1970);
74+
assert.strictEqual(new Date(1970, 0).getMonth(), 0);
75+
assert.strictEqual(new Date(1970, 0, 1).getDate(), 1);
76+
assert.strictEqual(new Date(1970, 0, 1, 11).getHours(), 11);
77+
assert.strictEqual(new Date(1970, 0, 1, 11, 10).getMinutes(), 10);
78+
assert.strictEqual(new Date(1970, 0, 1, 11, 10, 45).getSeconds(), 45);
79+
assert.strictEqual(new Date(1970, 0, 1, 11, 10, 45, 898).getMilliseconds(), 898);
80+
assert.strictEqual(new Date(1970, 0, 1, 11, 10, 45, 898).toDateString(), 'Thu Jan 01 1970');
81+
});
82+
83+
it('should return native code when calling Date.toString', (t) => {
84+
t.mock.timers.enable({ apis: ['Date'] });
85+
assert.strictEqual(Date.toString(), 'function Date() { [native code] }');
86+
});
87+
88+
it('should start with a custom epoch if the second argument is specified', (t) => {
89+
t.mock.timers.enable({ apis: ['Date'], now: 100 });
90+
const date1 = new Date();
91+
assert.strictEqual(date1.getTime(), 100);
92+
93+
t.mock.timers.reset();
94+
t.mock.timers.enable({ apis: ['Date'], now: new Date(200) });
95+
const date2 = new Date();
96+
assert.strictEqual(date2.getTime(), 200);
97+
});
98+
99+
it('should replace epoch if setTime is lesser than now and not tick', (t) => {
100+
t.mock.timers.enable();
101+
const fn = t.mock.fn();
102+
const id = setTimeout(fn, 1000);
103+
t.mock.timers.setTime(800);
104+
assert.strictEqual(Date.now(), 800);
105+
t.mock.timers.setTime(500);
106+
assert.strictEqual(Date.now(), 500);
107+
assert.strictEqual(fn.mock.callCount(), 0);
108+
clearTimeout(id);
109+
});
110+
111+
it('should not tick time when setTime is called', (t) => {
112+
t.mock.timers.enable();
113+
const fn = t.mock.fn();
114+
const id = setTimeout(fn, 1000);
115+
t.mock.timers.setTime(1200);
116+
assert.strictEqual(Date.now(), 1200);
117+
assert.strictEqual(fn.mock.callCount(), 0);
118+
clearTimeout(id);
119+
});
120+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
'use strict';
2+
process.env.NODE_TEST_KNOWN_GLOBALS = 0;
3+
const common = require('../common');
4+
5+
const assert = require('node:assert');
6+
const { it, describe } = require('node:test');
7+
const nodeTimersPromises = require('node:timers/promises');
8+
9+
describe('Mock Timers Scheduler Test Suite', () => {
10+
it('should advance in time and trigger timers when calling the .tick function', (t) => {
11+
t.mock.timers.enable({ apis: ['scheduler.wait'] });
12+
13+
const now = Date.now();
14+
const durationAtMost = 100;
15+
16+
const p = nodeTimersPromises.scheduler.wait(4000);
17+
t.mock.timers.tick(4000);
18+
19+
return p.then(common.mustCall((result) => {
20+
assert.strictEqual(result, undefined);
21+
assert.ok(
22+
Date.now() - now < durationAtMost,
23+
`time should be advanced less than the ${durationAtMost}ms`
24+
);
25+
}));
26+
});
27+
28+
it('should advance in time and trigger timers when calling the .tick function multiple times', async (t) => {
29+
t.mock.timers.enable({ apis: ['scheduler.wait'] });
30+
31+
const fn = t.mock.fn();
32+
33+
nodeTimersPromises.scheduler.wait(9999).then(fn);
34+
35+
t.mock.timers.tick(8999);
36+
assert.strictEqual(fn.mock.callCount(), 0);
37+
t.mock.timers.tick(500);
38+
39+
await nodeTimersPromises.setImmediate();
40+
41+
assert.strictEqual(fn.mock.callCount(), 0);
42+
t.mock.timers.tick(500);
43+
44+
await nodeTimersPromises.setImmediate();
45+
assert.strictEqual(fn.mock.callCount(), 1);
46+
});
47+
48+
it('should work with the same params as the original timers/promises/scheduler.wait', async (t) => {
49+
t.mock.timers.enable({ apis: ['scheduler.wait'] });
50+
const controller = new AbortController();
51+
const p = nodeTimersPromises.scheduler.wait(2000, {
52+
ref: true,
53+
signal: controller.signal,
54+
});
55+
56+
t.mock.timers.tick(1000);
57+
t.mock.timers.tick(500);
58+
t.mock.timers.tick(500);
59+
t.mock.timers.tick(500);
60+
61+
const result = await p;
62+
assert.strictEqual(result, undefined);
63+
});
64+
65+
it('should abort operation if timers/promises/scheduler.wait received an aborted signal', async (t) => {
66+
t.mock.timers.enable({ apis: ['scheduler.wait'] });
67+
const controller = new AbortController();
68+
const p = nodeTimersPromises.scheduler.wait(2000, {
69+
ref: true,
70+
signal: controller.signal,
71+
});
72+
73+
t.mock.timers.tick(1000);
74+
controller.abort();
75+
t.mock.timers.tick(500);
76+
t.mock.timers.tick(500);
77+
t.mock.timers.tick(500);
78+
79+
await assert.rejects(() => p, {
80+
name: 'AbortError',
81+
});
82+
});
83+
it('should abort operation even if the .tick was not called', async (t) => {
84+
t.mock.timers.enable({ apis: ['scheduler.wait'] });
85+
const controller = new AbortController();
86+
const p = nodeTimersPromises.scheduler.wait(2000, {
87+
ref: true,
88+
signal: controller.signal,
89+
});
90+
91+
controller.abort();
92+
93+
await assert.rejects(() => p, {
94+
name: 'AbortError',
95+
});
96+
});
97+
98+
it('should abort operation when .abort is called before calling setInterval', async (t) => {
99+
t.mock.timers.enable({ apis: ['scheduler.wait'] });
100+
const controller = new AbortController();
101+
controller.abort();
102+
const p = nodeTimersPromises.scheduler.wait(2000, {
103+
ref: true,
104+
signal: controller.signal,
105+
});
106+
107+
await assert.rejects(() => p, {
108+
name: 'AbortError',
109+
});
110+
});
111+
112+
it('should reject given an an invalid signal instance', async (t) => {
113+
t.mock.timers.enable({ apis: ['scheduler.wait'] });
114+
const p = nodeTimersPromises.scheduler.wait(2000, {
115+
ref: true,
116+
signal: {},
117+
});
118+
119+
await assert.rejects(() => p, {
120+
name: 'TypeError',
121+
code: 'ERR_INVALID_ARG_TYPE',
122+
});
123+
});
124+
});

0 commit comments

Comments
 (0)