mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
test_runner: test return value of mocked promisified timers
PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
1 parent
54ebfc10cb
commit
e2c3b015cd
2 files changed
+35
-17
No files matched your search
@@ -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) {
|
||||
@@ -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);
|
||||
|
||||
@@ -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';
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user