Skip to content
Open
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
8 changes: 5 additions & 3 deletions lib/internal/test_runner/mock/mock_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -799,10 +799,12 @@ class MockTimers {
this.#timersInContext = [];
this.#now = kInitialEpoch;

let timer = this.#executionQueue.peek();
let timer = this.#executionQueue.shift();
while (timer) {
this.#executionQueue.shift();
timer = this.#executionQueue.peek();
// Forget the queue position so that clearing this timer later does
// not remove whatever timer occupies that position afterwards.
timer.priorityQueuePosition = undefined;
timer = this.#executionQueue.shift();
}
}

Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-runner-mock-timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,36 @@ describe('Mock Timers Test Suite', () => {
nodeTimers.clearTimeout();
nodeTimers.clearTimeout(null);
});

it('clearing a timer from before a reset does not remove a new timer', (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
const stale = globalThis.setTimeout(common.mustNotCall(), 1000);
t.mock.timers.reset();

t.mock.timers.enable({ apis: ['setTimeout'] });
const fn = t.mock.fn();
globalThis.setTimeout(fn, 10);
globalThis.clearTimeout(stale);
t.mock.timers.runAll();

assert.strictEqual(fn.mock.callCount(), 1);
});

it('clearing a timer from another MockTimers instance does not remove a timer', (t) => {
const { MockTracker } = require('internal/test_runner/mock/mock');
const other = new MockTracker();
other.timers.enable({ apis: ['setTimeout'] });
const stale = globalThis.setTimeout(common.mustNotCall(), 1000);
other.reset();

t.mock.timers.enable({ apis: ['setTimeout'] });
const fn = t.mock.fn();
globalThis.setTimeout(fn, 10);
globalThis.clearTimeout(stale);
t.mock.timers.runAll();

assert.strictEqual(fn.mock.callCount(), 1);
});
});

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