Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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