Skip to content

Commit a5ebc89

Browse files
SRHerzogtargos
authored andcommitted
test_runner: support defining test reporter in NODE_OPTIONS
Adds --test-reporter and --test-reporter-destination as allowable options in NODE_OPTIONS. Also adds the CLI flag --test-child-process to allow forcing the default test-reporter for inter-process communication. Fixes: #46484 PR-URL: #46688 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent b3fe2ba commit a5ebc89

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

doc/api/cli.md

+7
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,8 @@ Node.js options that are allowed are:
19411941
* `--secure-heap`
19421942
* `--snapshot-blob`
19431943
* `--test-only`
1944+
* `--test-reporter-destination`
1945+
* `--test-reporter`
19441946
* `--throw-deprecation`
19451947
* `--title`
19461948
* `--tls-cipher-list`
@@ -2082,6 +2084,11 @@ If `value` equals `'1'`, the check for a supported platform is skipped during
20822084
Node.js startup. Node.js might not execute correctly. Any issues encountered
20832085
on unsupported platforms will not be fixed.
20842086

2087+
### `NODE_TEST_CONTEXT=value`
2088+
2089+
If `value` equals `'child'`, test reporter options will be overridden and test
2090+
output will be sent to stdout in the TAP format.
2091+
20852092
### `NODE_TLS_REJECT_UNAUTHORIZED=value`
20862093

20872094
If `value` equals `'0'`, certificate validation is disabled for TLS connections.

lib/internal/test_runner/runner.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ function getRunArgs({ path, inspectPort }) {
145145
ArrayPrototypePush(argv, `--inspect-port=${getInspectPort(inspectPort)}`);
146146
}
147147
ArrayPrototypePush(argv, path);
148+
148149
return argv;
149150
}
150151

@@ -260,7 +261,7 @@ function runTestFile(path, root, inspectPort, filesWatcher) {
260261
const subtest = root.createSubtest(FileTest, path, async (t) => {
261262
const args = getRunArgs({ path, inspectPort });
262263
const stdio = ['pipe', 'pipe', 'pipe'];
263-
const env = { ...process.env };
264+
const env = { ...process.env, NODE_TEST_CONTEXT: 'child' };
264265
if (filesWatcher) {
265266
stdio.push('ipc');
266267
env.WATCH_REPORT_DEPENDENCIES = '1';

lib/internal/test_runner/utils.js

+23-14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
RegExpPrototypeExec,
99
SafeMap,
1010
} = primordials;
11+
1112
const { basename } = require('path');
1213
const { createWriteStream } = require('fs');
1314
const { pathToFileURL } = require('internal/url');
@@ -166,25 +167,33 @@ function parseCommandLine() {
166167

167168
const isTestRunner = getOptionValue('--test');
168169
const coverage = getOptionValue('--experimental-test-coverage');
169-
const destinations = getOptionValue('--test-reporter-destination');
170-
const reporters = getOptionValue('--test-reporter');
170+
const isChildProcess = process.env.NODE_TEST_CONTEXT === 'child';
171+
let destinations;
172+
let reporters;
171173
let testNamePatterns;
172174
let testOnlyFlag;
173175

174-
if (reporters.length === 0 && destinations.length === 0) {
175-
ArrayPrototypePush(reporters, kDefaultReporter);
176-
}
176+
if (isChildProcess) {
177+
reporters = [kDefaultReporter];
178+
destinations = [kDefaultDestination];
179+
} else {
180+
destinations = getOptionValue('--test-reporter-destination');
181+
reporters = getOptionValue('--test-reporter');
182+
if (reporters.length === 0 && destinations.length === 0) {
183+
ArrayPrototypePush(reporters, kDefaultReporter);
184+
}
177185

178-
if (reporters.length === 1 && destinations.length === 0) {
179-
ArrayPrototypePush(destinations, kDefaultDestination);
180-
}
186+
if (reporters.length === 1 && destinations.length === 0) {
187+
ArrayPrototypePush(destinations, kDefaultDestination);
188+
}
181189

182-
if (destinations.length !== reporters.length) {
183-
throw new ERR_INVALID_ARG_VALUE(
184-
'--test-reporter',
185-
reporters,
186-
'must match the number of specified \'--test-reporter-destination\'',
187-
);
190+
if (destinations.length !== reporters.length) {
191+
throw new ERR_INVALID_ARG_VALUE(
192+
'--test-reporter',
193+
reporters,
194+
'must match the number of specified \'--test-reporter-destination\'',
195+
);
196+
}
188197
}
189198

190199
if (isTestRunner) {

src/node_options.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,12 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
569569
&EnvironmentOptions::test_name_pattern);
570570
AddOption("--test-reporter",
571571
"report test output using the given reporter",
572-
&EnvironmentOptions::test_reporter);
572+
&EnvironmentOptions::test_reporter,
573+
kAllowedInEnvvar);
573574
AddOption("--test-reporter-destination",
574575
"report given reporter to the given destination",
575-
&EnvironmentOptions::test_reporter_destination);
576+
&EnvironmentOptions::test_reporter_destination,
577+
kAllowedInEnvvar);
576578
AddOption("--test-only",
577579
"run tests with 'only' option set",
578580
&EnvironmentOptions::test_only,

0 commit comments

Comments
 (0)