Skip to content

Commit 3f8cc88

Browse files
Benricheson101MoLow
authored andcommitted
test_runner: fix clearing final timeout in own callback
PR-URL: #52332 Fixes: #52325 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 05cb16d commit 3f8cc88

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/internal/test_runner/mock/mock_timers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ class MockTimers {
652652

653653
// Check if the timeout was cleared by calling clearTimeout inside its own callback
654654
const afterCallback = this.#executionQueue.peek();
655-
if (afterCallback.id === timer.id) {
655+
if (afterCallback?.id === timer.id) {
656656
this.#executionQueue.shift();
657657
timer.priorityQueuePosition = undefined;
658658
}

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

+13
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,19 @@ describe('Mock Timers Test Suite', () => {
548548
t.mock.timers.runAll();
549549
assert.strictEqual(f.mock.callCount(), 3);
550550
});
551+
552+
it('should allow clearing timeout inside own callback', (t) => {
553+
t.mock.timers.enable({ apis: ['setTimeout'] });
554+
const f = t.mock.fn();
555+
556+
const timer = nodeTimers.setTimeout(() => {
557+
f();
558+
nodeTimers.clearTimeout(timer);
559+
}, 50);
560+
561+
t.mock.timers.runAll();
562+
assert.strictEqual(f.mock.callCount(), 1);
563+
});
551564
});
552565

553566
describe('setInterval Suite', () => {

0 commit comments

Comments
 (0)