Skip to content

Commit 0f8b826

Browse files
badkeyymarco-ippolito
authored andcommittedJan 24, 2025
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>

File tree

3 files changed

+248
-236
lines changed

3 files changed

+248
-236
lines changed
 

Diff for: ‎test/parallel/test-runner-mock-timers-date.js

+120
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+
});

Diff for: ‎test/parallel/test-runner-mock-timers-scheduler.js

+124
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+
});

Diff for: ‎test/parallel/test-runner-mock-timers.js

+4-236
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Flags: --expose-internals
12
'use strict';
23
process.env.NODE_TEST_KNOWN_GLOBALS = 0;
34
const common = require('../common');
@@ -6,6 +7,7 @@ const assert = require('node:assert');
67
const { it, mock, describe } = require('node:test');
78
const nodeTimers = require('node:timers');
89
const nodeTimersPromises = require('node:timers/promises');
10+
const { TIMEOUT_MAX } = require('internal/timers');
911

1012
describe('Mock Timers Test Suite', () => {
1113
describe('MockTimers API', () => {
@@ -252,10 +254,10 @@ describe('Mock Timers Test Suite', () => {
252254
}), timeout);
253255
});
254256

255-
it('should change timeout to 1ms when it is >= 2 ** 31', (t) => {
257+
it('should change timeout to 1ms when it is > TIMEOUT_MAX', (t) => {
256258
t.mock.timers.enable({ apis: ['setTimeout'] });
257259
const fn = t.mock.fn();
258-
global.setTimeout(fn, 2 ** 31);
260+
global.setTimeout(fn, TIMEOUT_MAX + 1);
259261
t.mock.timers.tick(1);
260262
assert.strictEqual(fn.mock.callCount(), 1);
261263
});
@@ -791,240 +793,6 @@ describe('Mock Timers Test Suite', () => {
791793
});
792794
});
793795

794-
describe('scheduler Suite', () => {
795-
describe('scheduler.wait', () => {
796-
it('should advance in time and trigger timers when calling the .tick function', (t) => {
797-
t.mock.timers.enable({ apis: ['scheduler.wait'] });
798-
799-
const now = Date.now();
800-
const durationAtMost = 100;
801-
802-
const p = nodeTimersPromises.scheduler.wait(4000);
803-
t.mock.timers.tick(4000);
804-
805-
return p.then(common.mustCall((result) => {
806-
assert.strictEqual(result, undefined);
807-
assert.ok(
808-
Date.now() - now < durationAtMost,
809-
`time should be advanced less than the ${durationAtMost}ms`
810-
);
811-
}));
812-
});
813-
814-
it('should advance in time and trigger timers when calling the .tick function multiple times', async (t) => {
815-
t.mock.timers.enable({ apis: ['scheduler.wait'] });
816-
817-
const fn = t.mock.fn();
818-
819-
nodeTimersPromises.scheduler.wait(9999).then(fn);
820-
821-
t.mock.timers.tick(8999);
822-
assert.strictEqual(fn.mock.callCount(), 0);
823-
t.mock.timers.tick(500);
824-
825-
await nodeTimersPromises.setImmediate();
826-
827-
assert.strictEqual(fn.mock.callCount(), 0);
828-
t.mock.timers.tick(500);
829-
830-
await nodeTimersPromises.setImmediate();
831-
assert.strictEqual(fn.mock.callCount(), 1);
832-
});
833-
834-
it('should work with the same params as the original timers/promises/scheduler.wait', async (t) => {
835-
t.mock.timers.enable({ apis: ['scheduler.wait'] });
836-
const controller = new AbortController();
837-
const p = nodeTimersPromises.scheduler.wait(2000, {
838-
ref: true,
839-
signal: controller.signal,
840-
});
841-
842-
t.mock.timers.tick(1000);
843-
t.mock.timers.tick(500);
844-
t.mock.timers.tick(500);
845-
t.mock.timers.tick(500);
846-
847-
const result = await p;
848-
assert.strictEqual(result, undefined);
849-
});
850-
851-
it('should abort operation if timers/promises/scheduler.wait received an aborted signal', async (t) => {
852-
t.mock.timers.enable({ apis: ['scheduler.wait'] });
853-
const controller = new AbortController();
854-
const p = nodeTimersPromises.scheduler.wait(2000, {
855-
ref: true,
856-
signal: controller.signal,
857-
});
858-
859-
t.mock.timers.tick(1000);
860-
controller.abort();
861-
t.mock.timers.tick(500);
862-
t.mock.timers.tick(500);
863-
t.mock.timers.tick(500);
864-
865-
await assert.rejects(() => p, {
866-
name: 'AbortError',
867-
});
868-
});
869-
it('should abort operation even if the .tick was not called', async (t) => {
870-
t.mock.timers.enable({ apis: ['scheduler.wait'] });
871-
const controller = new AbortController();
872-
const p = nodeTimersPromises.scheduler.wait(2000, {
873-
ref: true,
874-
signal: controller.signal,
875-
});
876-
877-
controller.abort();
878-
879-
await assert.rejects(() => p, {
880-
name: 'AbortError',
881-
});
882-
});
883-
884-
it('should abort operation when .abort is called before calling setInterval', async (t) => {
885-
t.mock.timers.enable({ apis: ['scheduler.wait'] });
886-
const controller = new AbortController();
887-
controller.abort();
888-
const p = nodeTimersPromises.scheduler.wait(2000, {
889-
ref: true,
890-
signal: controller.signal,
891-
});
892-
893-
await assert.rejects(() => p, {
894-
name: 'AbortError',
895-
});
896-
});
897-
898-
it('should reject given an an invalid signal instance', async (t) => {
899-
t.mock.timers.enable({ apis: ['scheduler.wait'] });
900-
const p = nodeTimersPromises.scheduler.wait(2000, {
901-
ref: true,
902-
signal: {},
903-
});
904-
905-
await assert.rejects(() => p, {
906-
name: 'TypeError',
907-
code: 'ERR_INVALID_ARG_TYPE',
908-
});
909-
});
910-
911-
});
912-
});
913-
914-
describe('Date Suite', () => {
915-
it('should return the initial UNIX epoch if not specified', (t) => {
916-
t.mock.timers.enable({ apis: ['Date'] });
917-
const date = new Date();
918-
assert.strictEqual(date.getTime(), 0);
919-
assert.strictEqual(Date.now(), 0);
920-
});
921-
922-
it('should throw an error if setTime is called without enabling timers', (t) => {
923-
assert.throws(
924-
() => {
925-
t.mock.timers.setTime(100);
926-
},
927-
{ code: 'ERR_INVALID_STATE' }
928-
);
929-
});
930-
931-
it('should throw an error if epoch passed to enable is not valid', (t) => {
932-
assert.throws(
933-
() => {
934-
t.mock.timers.enable({ now: -1 });
935-
},
936-
{ code: 'ERR_INVALID_ARG_VALUE' }
937-
);
938-
939-
assert.throws(
940-
() => {
941-
t.mock.timers.enable({ now: 'string' });
942-
},
943-
{ code: 'ERR_INVALID_ARG_TYPE' }
944-
);
945-
946-
assert.throws(
947-
() => {
948-
t.mock.timers.enable({ now: NaN });
949-
},
950-
{ code: 'ERR_INVALID_ARG_VALUE' }
951-
);
952-
});
953-
954-
it('should replace the original Date with the mocked one', (t) => {
955-
t.mock.timers.enable({ apis: ['Date'] });
956-
assert.ok(Date.isMock);
957-
});
958-
959-
it('should return the ticked time when calling Date.now after tick', (t) => {
960-
t.mock.timers.enable({ apis: ['Date'] });
961-
const time = 100;
962-
t.mock.timers.tick(time);
963-
assert.strictEqual(Date.now(), time);
964-
});
965-
966-
it('should return the Date as string when calling it as a function', (t) => {
967-
t.mock.timers.enable({ apis: ['Date'] });
968-
const returned = Date();
969-
// Matches the format: 'Mon Jan 01 1970 00:00:00'
970-
// We don't care about the date, just the format
971-
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));
972-
});
973-
974-
it('should return the date with different argument calls', (t) => {
975-
t.mock.timers.enable({ apis: ['Date'] });
976-
assert.strictEqual(new Date(0).getTime(), 0);
977-
assert.strictEqual(new Date(100).getTime(), 100);
978-
assert.strictEqual(new Date('1970-01-01T00:00:00.000Z').getTime(), 0);
979-
assert.strictEqual(new Date(1970, 0).getFullYear(), 1970);
980-
assert.strictEqual(new Date(1970, 0).getMonth(), 0);
981-
assert.strictEqual(new Date(1970, 0, 1).getDate(), 1);
982-
assert.strictEqual(new Date(1970, 0, 1, 11).getHours(), 11);
983-
assert.strictEqual(new Date(1970, 0, 1, 11, 10).getMinutes(), 10);
984-
assert.strictEqual(new Date(1970, 0, 1, 11, 10, 45).getSeconds(), 45);
985-
assert.strictEqual(new Date(1970, 0, 1, 11, 10, 45, 898).getMilliseconds(), 898);
986-
assert.strictEqual(new Date(1970, 0, 1, 11, 10, 45, 898).toDateString(), 'Thu Jan 01 1970');
987-
});
988-
989-
it('should return native code when calling Date.toString', (t) => {
990-
t.mock.timers.enable({ apis: ['Date'] });
991-
assert.strictEqual(Date.toString(), 'function Date() { [native code] }');
992-
});
993-
994-
it('should start with a custom epoch if the second argument is specified', (t) => {
995-
t.mock.timers.enable({ apis: ['Date'], now: 100 });
996-
const date1 = new Date();
997-
assert.strictEqual(date1.getTime(), 100);
998-
999-
t.mock.timers.reset();
1000-
t.mock.timers.enable({ apis: ['Date'], now: new Date(200) });
1001-
const date2 = new Date();
1002-
assert.strictEqual(date2.getTime(), 200);
1003-
});
1004-
1005-
it('should replace epoch if setTime is lesser than now and not tick', (t) => {
1006-
t.mock.timers.enable();
1007-
const fn = t.mock.fn();
1008-
const id = setTimeout(fn, 1000);
1009-
t.mock.timers.setTime(800);
1010-
assert.strictEqual(Date.now(), 800);
1011-
t.mock.timers.setTime(500);
1012-
assert.strictEqual(Date.now(), 500);
1013-
assert.strictEqual(fn.mock.callCount(), 0);
1014-
clearTimeout(id);
1015-
});
1016-
1017-
it('should not tick time when setTime is called', (t) => {
1018-
t.mock.timers.enable();
1019-
const fn = t.mock.fn();
1020-
const id = setTimeout(fn, 1000);
1021-
t.mock.timers.setTime(1200);
1022-
assert.strictEqual(Date.now(), 1200);
1023-
assert.strictEqual(fn.mock.callCount(), 0);
1024-
clearTimeout(id);
1025-
});
1026-
});
1027-
1028796
describe('Api should have same public properties as original', () => {
1029797
it('should have hasRef', (t) => {
1030798
t.mock.timers.enable();

0 commit comments

Comments
 (0)
Please sign in to comment.