From 4f943976508713aafc9acbfc52f11a219f9d847e Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Mon, 19 Aug 2024 12:40:50 +0900 Subject: [PATCH] test_runner: use validateStringArray for `timers.enable()` `apis` which is argument of `timers.enable()` is string array. So use `validatStringArray` instead of `validateArray`. And `options` is optional, so update JSDoc. PR-URL: https://github.com/nodejs/node/pull/49534 Reviewed-By: Luigi Pinca Reviewed-By: Antoine du Hamel Reviewed-By: Chemi Atlow Reviewed-By: Moshe Atlow Reviewed-By: Zijian Liu Reviewed-By: Benjamin Gruenbaum --- lib/internal/test_runner/mock/mock_timers.js | 6 +++--- test/parallel/test-runner-mock-timers.js | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/internal/test_runner/mock/mock_timers.js b/lib/internal/test_runner/mock/mock_timers.js index bacfbf41a2..400ca5e37f 100644 --- a/lib/internal/test_runner/mock/mock_timers.js +++ b/lib/internal/test_runner/mock/mock_timers.js @@ -23,8 +23,8 @@ const { const { validateAbortSignal, - validateArray, validateNumber, + validateStringArray, } = require('internal/validators'); const { @@ -676,7 +676,7 @@ class MockTimers { */ /** * Enables the MockTimers replacing the native timers with the fake ones. - * @param {EnableOptions} options + * @param {EnableOptions} [options] */ enable(options = { __proto__: null, apis: SUPPORTED_APIS, now: 0 }) { const internalOptions = { __proto__: null, ...options }; @@ -696,7 +696,7 @@ class MockTimers { internalOptions.apis = SUPPORTED_APIS; } - validateArray(internalOptions.apis, 'options.apis'); + validateStringArray(internalOptions.apis, 'options.apis'); // Check that the timers passed are supported ArrayPrototypeForEach(internalOptions.apis, (timer) => { if (!ArrayPrototypeIncludes(SUPPORTED_APIS, timer)) { diff --git a/test/parallel/test-runner-mock-timers.js b/test/parallel/test-runner-mock-timers.js index 3e8d2d79ed..e2a86a5263 100644 --- a/test/parallel/test-runner-mock-timers.js +++ b/test/parallel/test-runner-mock-timers.js @@ -17,6 +17,14 @@ describe('Mock Timers Test Suite', () => { }); }); + it('should throw an error if data type of trying to enable a timer is not string', (t) => { + assert.throws(() => { + t.mock.timers.enable({ apis: [1] }); + }, { + code: 'ERR_INVALID_ARG_TYPE', + }); + }); + it('should throw an error if trying to enable a timer twice', (t) => { t.mock.timers.enable(); assert.throws(() => {