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 and targos committed Oct 2, 2024
1 parent 1ac1fe4 commit 4f421b3
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 @@ -26,8 +26,8 @@ const {
} = primordials;
const {
validateAbortSignal,
validateArray,
validateNumber,
validateStringArray,
} = require('internal/validators');

const {
Expand Down Expand Up @@ -673,7 +673,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 @@ -693,7 +693,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 4f421b3

Please sign in to comment.