diff --git a/lib/internal/test_runner/mock/mock_timers.js b/lib/internal/test_runner/mock/mock_timers.js index 1e425b606ee5..55a22f5de5e7 100644 --- a/lib/internal/test_runner/mock/mock_timers.js +++ b/lib/internal/test_runner/mock/mock_timers.js @@ -698,16 +698,16 @@ class MockTimers { this.#assertTimersAreEnabled(); this.#assertTimeArg(time); - this.#now += time; + const target = this.#now + time; let timer = this.#executionQueue.peek(); while (timer) { - if (timer.runAt > this.#now) break; + if (timer.runAt > target) break; + if (timer.runAt > this.#now) this.#now = timer.runAt; ReflectApply(timer.callback, undefined, timer.args); // Check if the timeout was cleared by calling clearTimeout inside its own callback - const afterCallback = this.#executionQueue.peek(); - if (afterCallback?.id === timer.id) { - this.#executionQueue.shift(); + if (timer.priorityQueuePosition !== undefined) { + this.#executionQueue.removeAt(timer.priorityQueuePosition); timer.priorityQueuePosition = undefined; } @@ -718,6 +718,8 @@ class MockTimers { timer = this.#executionQueue.peek(); } + + this.#now = target; } /** diff --git a/test/parallel/test-runner-mock-timers-date.js b/test/parallel/test-runner-mock-timers-date.js index 06ae8817b102..331e04336ff6 100644 --- a/test/parallel/test-runner-mock-timers-date.js +++ b/test/parallel/test-runner-mock-timers-date.js @@ -121,6 +121,29 @@ describe('Mock Timers Date Test Suite', () => { clearTimeout(id); }); + it('should run each timer at its scheduled time when ticking', (t) => { + t.mock.timers.enable({ apis: ['setTimeout', 'setInterval', 'Date'] }); + const seen = []; + setTimeout(() => { + seen.push(`timeout@${Date.now()}`); + setTimeout(() => seen.push(`nested@${Date.now()}`), 50); + }, 100); + let count = 0; + const id = setInterval(() => { + seen.push(`interval@${Date.now()}`); + if (++count === 3) clearInterval(id); + }, 200); + t.mock.timers.tick(1000); + assert.deepStrictEqual(seen, [ + 'timeout@100', + 'nested@150', + 'interval@200', + 'interval@400', + 'interval@600', + ]); + assert.strictEqual(Date.now(), 1000); + }); + it((t) => { t.mock.timers.enable(); t.test('should throw when a already-mocked Date is mocked', (t2) => { diff --git a/test/parallel/test-runner-mock-timers.js b/test/parallel/test-runner-mock-timers.js index 25510fe023d0..42187a5fad8d 100644 --- a/test/parallel/test-runner-mock-timers.js +++ b/test/parallel/test-runner-mock-timers.js @@ -458,6 +458,17 @@ describe('Mock Timers Test Suite', () => { assert.deepStrictEqual(order, ['f1', 'f2']); }); + + it('should run setTimeout once if it calls setImmediate', (t) => { + t.mock.timers.enable({ apis: ['setImmediate', 'setTimeout'] }); + const fn = t.mock.fn(common.mustCall(1)); + + globalThis.setTimeout(common.mustCall(() => globalThis.setImmediate(fn), 1), 100); + + t.mock.timers.tick(500); + + assert.strictEqual(fn.mock.callCount(), 1); + }); }); describe('clearImmediate Suite', () => { diff --git a/test/parallel/test-util-throttle-timing.js b/test/parallel/test-util-throttle-timing.js index 56b6c7716a92..a1498f4d2b1a 100644 --- a/test/parallel/test-util-throttle-timing.js +++ b/test/parallel/test-util-throttle-timing.js @@ -132,7 +132,8 @@ process.on('unhandledRejection', common.mustNotCall()); mock.timers.tick(39); assert.deepStrictEqual(times, [0, 40]); if (delayed) { - mock.timers.tick(41); + mock.timers.setTime(Date.now() + 41); + mock.timers.tick(0); } else { mock.timers.tick(1); assert.deepStrictEqual(times, [0, 40, 80]);