Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 86f7cb8

Browse filesBrowse files
avivkellertargos
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
Copy full SHA for 86f7cb8

File tree

Expand file treeCollapse file tree

4 files changed

+44
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+44
-1
lines changed
Open diff view settings
Collapse file

‎doc/api/test.md‎

Copy file name to clipboardExpand all lines: doc/api/test.md
+6Lines changed: 6 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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.
Collapse file

‎lib/internal/test_runner/runner.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/runner.js
+19-1Lines changed: 19 additions & 1 deletion
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') {
Collapse file
+5Lines changed: 5 additions & 0 deletions
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+
});
Collapse file

‎test/parallel/test-runner-run.mjs‎

Copy file name to clipboardExpand all lines: test/parallel/test-runner-run.mjs
+14Lines changed: 14 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.