Skip to content

Commit e2c3b01

Browse files
mika-fischertargos
authored andcommitted
test_runner: test return value of mocked promisified timers
PR-URL: #50331 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Erick Wendel <erick.workspace@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 54ebfc1 commit e2c3b01

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

lib/internal/test_runner/mock/mock_timers.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ class MockTimers {
370370
return MockDate;
371371
}
372372

373-
async * #setIntervalPromisified(interval, startTime, options) {
373+
async * #setIntervalPromisified(interval, result, options) {
374374
const context = this;
375375
const emitter = new EventEmitter();
376376
if (options?.signal) {
@@ -394,8 +394,7 @@ class MockTimers {
394394

395395
const eventIt = EventEmitter.on(emitter, 'data');
396396
const callback = () => {
397-
startTime += interval;
398-
emitter.emit('data', startTime);
397+
emitter.emit('data', result);
399398
};
400399

401400
const timerId = this.#createTimer(true, callback, interval, options);

test/parallel/test-runner-mock-timers.js

+33-14
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,16 @@ describe('Mock Timers Test Suite', () => {
407407
assert.strictEqual(result, expectedResult);
408408
});
409409

410+
it('should always return the same result as the original timers/promises/setTimeout', async (t) => {
411+
t.mock.timers.enable({ apis: ['setTimeout'] });
412+
for (const expectedResult of [undefined, null, false, true, 0, 0n, 1, 1n, '', 'result', {}]) {
413+
const p = nodeTimersPromises.setTimeout(2000, expectedResult);
414+
t.mock.timers.tick(2000);
415+
const result = await p;
416+
assert.strictEqual(result, expectedResult);
417+
}
418+
});
419+
410420
it('should abort operation if timers/promises/setTimeout received an aborted signal', async (t) => {
411421
t.mock.timers.enable({ apis: ['setTimeout'] });
412422
const expectedResult = 'result';
@@ -505,10 +515,11 @@ describe('Mock Timers Test Suite', () => {
505515

506516
const expectedIterations = 5;
507517
const interval = 1000;
508-
const startedAt = Date.now();
518+
let time = 0;
509519
async function run() {
510520
const times = [];
511-
for await (const time of nodeTimersPromises.setInterval(interval, startedAt)) {
521+
for await (const _ of nodeTimersPromises.setInterval(interval)) { // eslint-disable-line no-unused-vars
522+
time += interval;
512523
times.push(time);
513524
if (times.length === expectedIterations) break;
514525
}
@@ -525,7 +536,20 @@ describe('Mock Timers Test Suite', () => {
525536
const timeResults = await r;
526537
assert.strictEqual(timeResults.length, expectedIterations);
527538
for (let it = 1; it < expectedIterations; it++) {
528-
assert.strictEqual(timeResults[it - 1], startedAt + (interval * it));
539+
assert.strictEqual(timeResults[it - 1], interval * it);
540+
}
541+
});
542+
543+
it('should always return the same result as the original timers/promises/setInterval', async (t) => {
544+
t.mock.timers.enable({ apis: ['setInterval'] });
545+
for (const expectedResult of [undefined, null, false, true, 0, 0n, 1, 1n, '', 'result', {}]) {
546+
const intervalIterator = nodeTimersPromises.setInterval(2000, expectedResult);
547+
const p = intervalIterator.next();
548+
t.mock.timers.tick(2000);
549+
const result = await p;
550+
await intervalIterator.return();
551+
assert.strictEqual(result.done, false);
552+
assert.strictEqual(result.value, expectedResult);
529553
}
530554
});
531555

@@ -579,13 +603,12 @@ describe('Mock Timers Test Suite', () => {
579603
const signal = controller.signal;
580604
const interval = 200;
581605
const expectedIterations = 2;
582-
const startedAt = Date.now();
583-
const timeResults = [];
606+
let numIterations = 0;
584607
async function run() {
585-
const it = nodeTimersPromises.setInterval(interval, startedAt, { signal });
586-
for await (const time of it) {
587-
timeResults.push(time);
588-
if (timeResults.length === 5) break;
608+
const it = nodeTimersPromises.setInterval(interval, undefined, { signal });
609+
for await (const _ of it) { // eslint-disable-line no-unused-vars
610+
numIterations += 1;
611+
if (numIterations === 5) break;
589612
}
590613
}
591614

@@ -601,11 +624,7 @@ describe('Mock Timers Test Suite', () => {
601624
await assert.rejects(() => r, {
602625
name: 'AbortError',
603626
});
604-
assert.strictEqual(timeResults.length, expectedIterations);
605-
606-
for (let it = 1; it < expectedIterations; it++) {
607-
assert.strictEqual(timeResults[it - 1], startedAt + (interval * it));
608-
}
627+
assert.strictEqual(numIterations, expectedIterations);
609628
});
610629
});
611630
});

0 commit comments

Comments
 (0)