Skip to content

Commit 76bf6b9

Browse files
hotpineapplerichardlau
authored andcommitted
test_runner: set mock timer's interval undefined
prevent adding timer to execution queue if clearInterval() called PR-URL: #59479 Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent 31ee7fc commit 76bf6b9

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/internal/test_runner/mock/mock_timers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ class MockTimers {
328328
if (timer?.priorityQueuePosition !== undefined) {
329329
this.#executionQueue.removeAt(timer.priorityQueuePosition);
330330
timer.priorityQueuePosition = undefined;
331+
timer.interval = undefined;
331332
}
332333
}
333334

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,27 @@ describe('Mock Timers Test Suite', () => {
201201
// Should not throw
202202
});
203203
});
204+
205+
it('interval cleared inside callback should only fire once', (t) => {
206+
t.mock.timers.enable();
207+
const calls = [];
208+
209+
setInterval(() => {
210+
calls.push('foo');
211+
}, 10);
212+
const timerId = setInterval(() => {
213+
calls.push('bar');
214+
clearInterval(timerId);
215+
}, 10);
216+
217+
t.mock.timers.tick(10);
218+
t.mock.timers.tick(10);
219+
220+
assert.deepStrictEqual(
221+
calls,
222+
['foo', 'bar', 'foo'],
223+
);
224+
});
204225
});
205226

206227
describe('globals/timers', () => {

0 commit comments

Comments
 (0)