Skip to content

Commit 86f7cb8

Browse files
Aviv Kellertargos
Aviv Keller
authored andcommitted
test_runner: support custom arguments in run()
PR-URL: #55126 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 6cc4951 commit 86f7cb8

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

doc/api/test.md

+6
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,12 @@ changes:
12991299
* `setup` {Function} A function that accepts the `TestsStream` instance
13001300
and can be used to setup listeners before any tests are run.
13011301
**Default:** `undefined`.
1302+
* `execArgv` {Array} An array of CLI flags to pass to the `node` executable when
1303+
spawning the subprocesses. This option has no effect when `isolation` is `'none`'.
1304+
**Default:** `[]`
1305+
* `argv` {Array} An array of CLI flags to pass to each test file when spawning the
1306+
subprocesses. This option has no effect when `isolation` is `'none'`.
1307+
**Default:** `[]`.
13021308
* `signal` {AbortSignal} Allows aborting an in-progress test execution.
13031309
* `testNamePatterns` {string|RegExp|Array} A String, RegExp or a RegExp Array,
13041310
that can be used to only run tests whose name matches the provided pattern.

lib/internal/test_runner/runner.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
ArrayPrototypeJoin,
1111
ArrayPrototypeMap,
1212
ArrayPrototypePush,
13+
ArrayPrototypePushApply,
1314
ArrayPrototypeShift,
1415
ArrayPrototypeSlice,
1516
ArrayPrototypeSome,
@@ -130,7 +131,13 @@ function filterExecArgv(arg, i, arr) {
130131
!ArrayPrototypeSome(kFilterArgValues, (p) => arg === p || (i > 0 && arr[i - 1] === p) || StringPrototypeStartsWith(arg, `${p}=`));
131132
}
132133

133-
function getRunArgs(path, { forceExit, inspectPort, testNamePatterns, testSkipPatterns, only }) {
134+
function getRunArgs(path, { forceExit,
135+
inspectPort,
136+
testNamePatterns,
137+
testSkipPatterns,
138+
only,
139+
argv: suppliedArgs,
140+
execArgv }) {
134141
const argv = ArrayPrototypeFilter(process.execArgv, filterExecArgv);
135142
if (forceExit === true) {
136143
ArrayPrototypePush(argv, '--test-force-exit');
@@ -148,12 +155,16 @@ function getRunArgs(path, { forceExit, inspectPort, testNamePatterns, testSkipPa
148155
ArrayPrototypePush(argv, '--test-only');
149156
}
150157

158+
ArrayPrototypePushApply(argv, execArgv);
159+
151160
if (path === kIsolatedProcessName) {
152161
ArrayPrototypePush(argv, '--test', ...ArrayPrototypeSlice(process.argv, 1));
153162
} else {
154163
ArrayPrototypePush(argv, path);
155164
}
156165

166+
ArrayPrototypePushApply(argv, suppliedArgs);
167+
157168
return argv;
158169
}
159170

@@ -548,6 +559,8 @@ function run(options = kEmptyObject) {
548559
lineCoverage = 0,
549560
branchCoverage = 0,
550561
functionCoverage = 0,
562+
execArgv = [],
563+
argv = [],
551564
} = options;
552565

553566
if (files != null) {
@@ -643,6 +656,9 @@ function run(options = kEmptyObject) {
643656
validateInteger(branchCoverage, 'options.branchCoverage', 0, 100);
644657
validateInteger(functionCoverage, 'options.functionCoverage', 0, 100);
645658

659+
validateStringArray(argv, 'options.argv');
660+
validateStringArray(execArgv, 'options.execArgv');
661+
646662
const rootTestOptions = { __proto__: null, concurrency, timeout, signal };
647663
const globalOptions = {
648664
__proto__: null,
@@ -685,6 +701,8 @@ function run(options = kEmptyObject) {
685701
forceExit,
686702
cwd,
687703
isolation,
704+
argv,
705+
execArgv,
688706
};
689707

690708
if (isolation === 'process') {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { test } = require('node:test');
2+
3+
test('process.argv is setup', (t) => {
4+
t.assert.deepStrictEqual(process.argv.slice(2), ['--a-custom-argument']);
5+
});

test/parallel/test-runner-run.mjs

+14
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
3333
for await (const _ of stream);
3434
});
3535

36+
const argPrintingFile = join(testFixtures, 'print-arguments.js');
37+
it('should allow custom arguments via execArgv', async () => {
38+
const result = await run({ files: [argPrintingFile], execArgv: ['-p', '"Printed"'] }).compose(spec).toArray();
39+
assert.strictEqual(result[0].toString(), 'Printed\n');
40+
});
41+
42+
it('should allow custom arguments via argv', async () => {
43+
const stream = run({ files: [argPrintingFile], argv: ['--a-custom-argument'] });
44+
stream.on('test:fail', common.mustNotCall());
45+
stream.on('test:pass', common.mustCall());
46+
// eslint-disable-next-line no-unused-vars
47+
for await (const _ of stream);
48+
});
49+
3650
it('should run same file twice', async () => {
3751
const stream = run({
3852
files: [

0 commit comments

Comments
 (0)