Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const { once } = require('events');
const { AbortController } = require('internal/abort_controller');
const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_TEST_FAILURE,
},
kIsNodeError,
Expand All @@ -33,9 +34,9 @@ const {
} = require('internal/util');
const { isPromise } = require('internal/util/types');
const {
isUint32,
validateAbortSignal,
validateNumber,
validateUint32,
} = require('internal/validators');
const { setTimeout } = require('timers/promises');
const { TIMEOUT_MAX } = require('internal/timers');
Expand Down Expand Up @@ -149,14 +150,23 @@ class Test extends AsyncResource {
this.timeout = parent.timeout;
}

if (isUint32(concurrency) && concurrency !== 0) {
this.concurrency = concurrency;
} else if (typeof concurrency === 'boolean') {
if (concurrency) {
this.concurrency = isTestRunner ? MathMax(cpus().length - 1, 1) : Infinity;
} else {
this.concurrency = 1;
}
switch (typeof concurrency) {
case 'number':
validateUint32(concurrency, 'options.concurrency', 1);
this.concurrency = concurrency;
break;

case 'boolean':
if (concurrency) {
this.concurrency = isTestRunner ? MathMax(cpus().length - 1, 1) : Infinity;
} else {
this.concurrency = 1;
}
break;

default:
if (concurrency != null)
throw new ERR_INVALID_ARG_TYPE('options.concurrency', ['boolean', 'number'], concurrency);
}

if (timeout != null && timeout !== Infinity) {
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-runner-option-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ const test = require('node:test');
// Valid values should not throw.
test({ timeout });
});

[Symbol(), {}, [], () => {}, 1n, '1'].forEach((concurrency) => {
assert.throws(() => test({ concurrency }), { code: 'ERR_INVALID_ARG_TYPE' });
});
[-1, 0, 1.1, -Infinity, NaN, 2 ** 33, Number.MAX_SAFE_INTEGER].forEach((concurrency) => {
assert.throws(() => test({ concurrency }), { code: 'ERR_OUT_OF_RANGE' });
});
[null, undefined, 1, 2 ** 31, true, false].forEach((concurrency) => {
// Valid values should not throw.
test({ concurrency });
});