test_runner: set mock timer's interval undefined

prevent adding timer to execution queue if clearInterval() called

PR-URL: https://github.com/nodejs/node/pull/59479
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
This commit is contained in:
hotpineapple 2025-08-27 12:36:54 +09:00 committed by GitHub
parent 0a46aa9e5d
commit ec8c73d6ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 0 deletions

View File

@ -321,6 +321,7 @@ class MockTimers {
if (timer?.priorityQueuePosition !== undefined) {
this.#executionQueue.removeAt(timer.priorityQueuePosition);
timer.priorityQueuePosition = undefined;
timer.interval = undefined;
}
}

View File

@ -201,6 +201,27 @@ describe('Mock Timers Test Suite', () => {
// Should not throw
});
});
it('interval cleared inside callback should only fire once', (t) => {
t.mock.timers.enable();
const calls = [];
setInterval(() => {
calls.push('foo');
}, 10);
const timerId = setInterval(() => {
calls.push('bar');
clearInterval(timerId);
}, 10);
t.mock.timers.tick(10);
t.mock.timers.tick(10);
assert.deepStrictEqual(
calls,
['foo', 'bar', 'foo'],
);
});
});
describe('globals/timers', () => {