Skip to content

Commit 68a4c6b

Browse files
committed
lib: set mock timer's interval undefined
prevent adding timer to execution queue if clearinterval called
1 parent cd1a90d commit 68a4c6b

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,28 @@ 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+
let timerId;
210+
setInterval(() => {
211+
calls.push('foo');
212+
}, 10);
213+
timerId = setInterval(() => {
214+
calls.push('bar');
215+
clearInterval(timerId);
216+
}, 10);
217+
218+
t.mock.timers.tick(10);
219+
t.mock.timers.tick(10);
220+
221+
assert.deepStrictEqual(
222+
calls,
223+
['foo', 'bar', 'foo'],
224+
);
225+
});
204226
});
205227

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

0 commit comments

Comments
 (0)