Skip to content

Commit 2417797

Browse files
hotpineappletargos
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 <[email protected]>
1 parent 19d633f commit 2417797

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
@@ -321,6 +321,7 @@ class MockTimers {
321321
if (timer?.priorityQueuePosition !== undefined) {
322322
this.#executionQueue.removeAt(timer.priorityQueuePosition);
323323
timer.priorityQueuePosition = undefined;
324+
timer.interval = undefined;
324325
}
325326
}
326327

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)