Skip to content

Commit ba15590

Browse files
committed
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. In document, some default value of `timers` are missed such as `setImmediate` and `clearImmediate`. Next, `milliseconds` which is argument of `timers.tick() is optional and default is 1.
1 parent 64c6d97 commit ba15590

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

doc/api/test.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,10 +1908,11 @@ mock.timers.enable({ apis: ['Date'], now: new Date() });
19081908

19091909
Alternatively, if you call `mock.timers.enable()` without any parameters:
19101910

1911-
All timers (`'setInterval'`, `'clearInterval'`, `'setTimeout'`, and `'clearTimeout'`)
1912-
will be mocked. The `setInterval`, `clearInterval`, `setTimeout`, and `clearTimeout`
1913-
functions from `node:timers`, `node:timers/promises`,
1914-
and `globalThis` will be mocked. As well as the global `Date` object.
1911+
All timers (`'setInterval'`, `'clearInterval'`, `'setTimeout'`, `'clearTimeout'`,
1912+
`'setImmediate'`, and `'clearImmediate'`) will be mocked. The `setInterval`,
1913+
`clearInterval`, `setTimeout`, `clearTimeout`, `'setImmediate'`, and
1914+
`'clearImmediate'` functions from `node:timers`, `node:timers/promises`, and
1915+
`globalThis` will be mocked. As well as the global `Date` object.
19151916

19161917
### `timers.reset()`
19171918

@@ -1942,7 +1943,7 @@ mock.timers.reset();
19421943

19431944
Calls `timers.reset()`.
19441945

1945-
### `timers.tick(milliseconds)`
1946+
### `timers.tick([milliseconds])`
19461947

19471948
<!-- YAML
19481949
added:
@@ -1953,7 +1954,7 @@ added:
19531954
Advances time for all mocked timers.
19541955

19551956
* `milliseconds` {number} The amount of time, in milliseconds,
1956-
to advance the timers.
1957+
to advance the timers. **Default:** `1`.
19571958

19581959
**Note:** This diverges from how `setTimeout` in Node.js behaves and accepts
19591960
only positive numbers. In Node.js, `setTimeout` with negative numbers is

lib/internal/test_runner/mock/mock_timers.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const {
2828
validateAbortSignal,
2929
validateArray,
3030
validateNumber,
31+
validateStringArray,
3132
} = require('internal/validators');
3233

3334
const {
@@ -641,7 +642,7 @@ class MockTimers {
641642
*/
642643
/**
643644
* Enables the MockTimers replacing the native timers with the fake ones.
644-
* @param {EnableOptions} options
645+
* @param {EnableOptions} [options]
645646
*/
646647
enable(options = { __proto__: null, apis: SUPPORTED_APIS, now: 0 }) {
647648
const internalOptions = { __proto__: null, ...options };
@@ -661,7 +662,7 @@ class MockTimers {
661662
internalOptions.apis = SUPPORTED_APIS;
662663
}
663664

664-
validateArray(internalOptions.apis, 'options.apis');
665+
validateStringArray(internalOptions.apis, 'options.apis');
665666
// Check that the timers passed are supported
666667
ArrayPrototypeForEach(internalOptions.apis, (timer) => {
667668
if (!ArrayPrototypeIncludes(SUPPORTED_APIS, timer)) {

0 commit comments

Comments
 (0)