Skip to content

Commit

Permalink
test_runner: use validateStringArray for timers.enable()
Browse files Browse the repository at this point in the history
`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: #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>
  • Loading branch information
deokjinkim authored Aug 19, 2024
1 parent 80dac9e commit 4f94397
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/internal/test_runner/mock/mock_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const {

const {
validateAbortSignal,
validateArray,
validateNumber,
validateStringArray,
} = require('internal/validators');

const {
Expand Down Expand Up @@ -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 };
Expand All @@ -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)) {
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-runner-mock-timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down

0 comments on commit 4f94397

Please sign in to comment.