Skip to content

Commit 5ef6f6d

Browse files
committed
accept non-UInt32 values as valid timeout
1 parent 44debf0 commit 5ef6f6d

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

lib/internal/test_runner/test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ const {
2727
kEmptyObject,
2828
} = require('internal/util');
2929
const { isPromise } = require('internal/util/types');
30-
const { isUint32, validateUint32 } = require('internal/validators');
30+
const { isUint32, validateNumber } = require('internal/validators');
3131
const { setTimeout } = require('timers/promises');
32+
const { TIMEOUT_MAX } = require('internal/timers');
3233
const { cpus } = require('os');
3334
const { bigint: hrtime } = process.hrtime;
3435
const kCallbackAndPromisePresent = 'callbackAndPromisePresent';
@@ -139,7 +140,7 @@ class Test extends AsyncResource {
139140
}
140141

141142
if (timeout != null && timeout !== Infinity) {
142-
validateUint32(timeout, 'options.timeout');
143+
validateNumber(timeout, 'options.timeout', 0, TIMEOUT_MAX);
143144
this.timeout = timeout;
144145
}
145146

lib/internal/validators.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
ArrayPrototypeJoin,
77
ArrayPrototypeMap,
88
NumberIsInteger,
9+
NumberIsNaN,
910
NumberMAX_SAFE_INTEGER,
1011
NumberMIN_SAFE_INTEGER,
1112
NumberParseInt,
@@ -115,9 +116,14 @@ function validateString(value, name) {
115116
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
116117
}
117118

118-
function validateNumber(value, name) {
119+
function validateNumber(value, name, min = undefined, max) {
119120
if (typeof value !== 'number')
120121
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
122+
123+
if ((min != null && value < min) || (max != null && value > max) ||
124+
((min != null || max != null) && NumberIsNaN(value))) {
125+
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
126+
}
121127
}
122128

123129
const validateOneOf = hideStackFrames((value, name, oneOf) => {

test/parallel/test-runner-option-validation.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const test = require('node:test');
66
[Symbol(), {}, [], () => {}, 1n, true, '1'].forEach((timeout) => {
77
assert.throws(() => test({ timeout }), { code: 'ERR_INVALID_ARG_TYPE' });
88
});
9-
[-1, 2 ** 33, 1.1, -Infinity, NaN].forEach((timeout) => {
9+
[-1, 2 ** 33, -Infinity, NaN].forEach((timeout) => {
1010
assert.throws(() => test({ timeout }), { code: 'ERR_OUT_OF_RANGE' });
1111
});
12-
[null, undefined, Infinity, 0, 1].forEach((timeout) => {
12+
[null, undefined, Infinity, 0, 1, 1.1].forEach((timeout) => {
1313
// Valid values should not throw.
1414
test({ timeout });
1515
});

0 commit comments

Comments
 (0)