test_runner: set mock timer's interval undefined · nodejs/node@2417797

File tree

2 files changed

lines changed

  • lib/internal/test_runner/mock

2 files changed

lines changed

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
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', () => {