Skip to content

Commit

Permalink
test_runner: test return value of mocked promisified timers
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mika-fischer authored and UlisesGascon committed Dec 11, 2023
1 parent cfd50f2 commit 4aaaff4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
5 changes: 2 additions & 3 deletions lib/internal/test_runner/mock/mock_timers.js
Expand Up @@ -370,7 +370,7 @@ class MockTimers {
return MockDate;
}

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

const eventIt = EventEmitter.on(emitter, 'data');
const callback = () => {
startTime += interval;
emitter.emit('data', startTime);
emitter.emit('data', result);
};

const timerId = this.#createTimer(true, callback, interval, options);
Expand Down
47 changes: 33 additions & 14 deletions test/parallel/test-runner-mock-timers.js
Expand Up @@ -407,6 +407,16 @@ describe('Mock Timers Test Suite', () => {
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';
Expand Down Expand Up @@ -505,10 +515,11 @@ describe('Mock Timers Test Suite', () => {

const expectedIterations = 5;
const interval = 1000;
const startedAt = Date.now();
let time = 0;
async function run() {
const times = [];
for await (const time of nodeTimersPromises.setInterval(interval, startedAt)) {
for await (const _ of nodeTimersPromises.setInterval(interval)) { // eslint-disable-line no-unused-vars
time += interval;
times.push(time);
if (times.length === expectedIterations) break;
}
Expand All @@ -525,7 +536,20 @@ describe('Mock Timers Test Suite', () => {
const timeResults = await r;
assert.strictEqual(timeResults.length, expectedIterations);
for (let it = 1; it < expectedIterations; it++) {
assert.strictEqual(timeResults[it - 1], startedAt + (interval * 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);
}
});

Expand Down Expand Up @@ -579,13 +603,12 @@ describe('Mock Timers Test Suite', () => {
const signal = controller.signal;
const interval = 200;
const expectedIterations = 2;
const startedAt = Date.now();
const timeResults = [];
let numIterations = 0;
async function run() {
const it = nodeTimersPromises.setInterval(interval, startedAt, { signal });
for await (const time of it) {
timeResults.push(time);
if (timeResults.length === 5) break;
const it = nodeTimersPromises.setInterval(interval, undefined, { signal });
for await (const _ of it) { // eslint-disable-line no-unused-vars
numIterations += 1;
if (numIterations === 5) break;
}
}

Expand All @@ -601,11 +624,7 @@ describe('Mock Timers Test Suite', () => {
await assert.rejects(() => r, {
name: 'AbortError',
});
assert.strictEqual(timeResults.length, expectedIterations);

for (let it = 1; it < expectedIterations; it++) {
assert.strictEqual(timeResults[it - 1], startedAt + (interval * it));
}
assert.strictEqual(numIterations, expectedIterations);
});
});
});
Expand Down

0 comments on commit 4aaaff4

Please sign in to comment.