From c52bc4e7633174ff83c9138f83a225274a1f9f02 Mon Sep 17 00:00:00 2001 From: Xunya Date: Tue, 23 Jul 2024 18:05:25 +0300 Subject: [PATCH] test_runner: do not throw on mocked clearTimeout() --- lib/internal/test_runner/mock/mock_timers.js | 2 +- test/parallel/test-runner-mock-timers.js | 23 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/internal/test_runner/mock/mock_timers.js b/lib/internal/test_runner/mock/mock_timers.js index 35c9a1ab68aac8..bacfbf41a207d6 100644 --- a/lib/internal/test_runner/mock/mock_timers.js +++ b/lib/internal/test_runner/mock/mock_timers.js @@ -304,7 +304,7 @@ class MockTimers { } #clearTimer(timer) { - if (timer.priorityQueuePosition !== undefined) { + if (timer?.priorityQueuePosition !== undefined) { this.#executionQueue.removeAt(timer.priorityQueuePosition); timer.priorityQueuePosition = undefined; } diff --git a/test/parallel/test-runner-mock-timers.js b/test/parallel/test-runner-mock-timers.js index 6ce6c28c95e326..3e8d2d79ede52b 100644 --- a/test/parallel/test-runner-mock-timers.js +++ b/test/parallel/test-runner-mock-timers.js @@ -257,6 +257,13 @@ describe('Mock Timers Test Suite', () => { assert.strictEqual(fn.mock.callCount(), 0); }); + + it('clearTimeout does not throw on null and undefined', (t) => { + t.mock.timers.enable({ apis: ['setTimeout'] }); + + nodeTimers.clearTimeout(); + nodeTimers.clearTimeout(null); + }); }); describe('setInterval Suite', () => { @@ -305,6 +312,13 @@ describe('Mock Timers Test Suite', () => { assert.strictEqual(fn.mock.callCount(), 0); }); + + it('clearInterval does not throw on null and undefined', (t) => { + t.mock.timers.enable({ apis: ['setInterval'] }); + + nodeTimers.clearInterval(); + nodeTimers.clearInterval(null); + }); }); describe('setImmediate Suite', () => { @@ -372,6 +386,15 @@ describe('Mock Timers Test Suite', () => { }); }); + describe('clearImmediate Suite', () => { + it('clearImmediate does not throw on null and undefined', (t) => { + t.mock.timers.enable({ apis: ['setImmediate'] }); + + nodeTimers.clearImmediate(); + nodeTimers.clearImmediate(null); + }); + }); + describe('timers/promises', () => { describe('setTimeout Suite', () => { it('should advance in time and trigger timers when calling the .tick function multiple times', async (t) => {