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 <luigipinca@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
Deokjin Kim
2024-08-19 12:40:50 +09:00
committed by GitHub
parent 80dac9e087
commit 4f94397650
2 changed files with 11 additions and 3 deletions

View File

@@ -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)) {

View File

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