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.
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.
  • Loading branch information
deokjinkim committed Jan 29, 2024
1 parent 64c6d97 commit 40e1c33
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
13 changes: 7 additions & 6 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -1908,10 +1908,11 @@ mock.timers.enable({ apis: ['Date'], now: new Date() });

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

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

### `timers.reset()`

Expand Down Expand Up @@ -1942,7 +1943,7 @@ mock.timers.reset();

Calls `timers.reset()`.

### `timers.tick(milliseconds)`
### `timers.tick([milliseconds])`

<!-- YAML
added:
Expand All @@ -1953,7 +1954,7 @@ added:
Advances time for all mocked timers.

* `milliseconds` {number} The amount of time, in milliseconds,
to advance the timers.
to advance the timers. **Default:** `1`.

**Note:** This diverges from how `setTimeout` in Node.js behaves and accepts
only positive numbers. In Node.js, `setTimeout` with negative numbers is
Expand Down
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 @@ -641,7 +641,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 @@ -661,7 +661,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

0 comments on commit 40e1c33

Please sign in to comment.