Skip to content

Commit

Permalink
test_runner: fix clearing final timeout in own callback
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Benricheson101 authored and marco-ippolito committed May 3, 2024
1 parent 5b758b9 commit 9497097
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/internal/test_runner/mock/mock_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ class MockTimers {

// Check if the timeout was cleared by calling clearTimeout inside its own callback
const afterCallback = this.#executionQueue.peek();
if (afterCallback.id === timer.id) {
if (afterCallback?.id === timer.id) {
this.#executionQueue.shift();
timer.priorityQueuePosition = undefined;
}
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-runner-mock-timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,19 @@ describe('Mock Timers Test Suite', () => {
t.mock.timers.runAll();
assert.strictEqual(f.mock.callCount(), 3);
});

it('should allow clearing timeout inside own callback', (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const f = t.mock.fn();

const timer = nodeTimers.setTimeout(() => {
f();
nodeTimers.clearTimeout(timer);
}, 50);

t.mock.timers.runAll();
assert.strictEqual(f.mock.callCount(), 1);
});
});

describe('setInterval Suite', () => {
Expand Down

0 comments on commit 9497097

Please sign in to comment.