Skip to content

Commit ddf819f

Browse files
SRHerzogdanielleadams
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent d8d2d33 commit ddf819f

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

doc/api/cli.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,8 @@ Node.js options that are allowed are:
19151915
* `--secure-heap`
19161916
* `--snapshot-blob`
19171917
* `--test-only`
1918+
* `--test-reporter-destination`
1919+
* `--test-reporter`
19181920
* `--throw-deprecation`
19191921
* `--title`
19201922
* `--tls-cipher-list`
@@ -2056,6 +2058,11 @@ If `value` equals `'1'`, the check for a supported platform is skipped during
20562058
Node.js startup. Node.js might not execute correctly. Any issues encountered
20572059
on unsupported platforms will not be fixed.
20582060

2061+
### `NODE_TEST_CONTEXT=value`
2062+
2063+
If `value` equals `'child'`, test reporter options will be overridden and test
2064+
output will be sent to stdout in the TAP format.
2065+
20592066
### `NODE_TLS_REJECT_UNAUTHORIZED=value`
20602067

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

lib/internal/test_runner/runner.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ function getRunArgs({ path, inspectPort }) {
144144
ArrayPrototypePush(argv, `--inspect-port=${getInspectPort(inspectPort)}`);
145145
}
146146
ArrayPrototypePush(argv, path);
147+
147148
return argv;
148149
}
149150

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

lib/internal/test_runner/utils.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
RegExpPrototypeExec,
1010
SafeMap,
1111
} = primordials;
12+
1213
const { basename } = require('path');
1314
const { createWriteStream } = require('fs');
1415
const { pathToFileURL } = require('internal/url');
@@ -167,25 +168,33 @@ function parseCommandLine() {
167168

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

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

179-
if (reporters.length === 1 && destinations.length === 0) {
180-
ArrayPrototypePush(destinations, kDefaultDestination);
181-
}
187+
if (reporters.length === 1 && destinations.length === 0) {
188+
ArrayPrototypePush(destinations, kDefaultDestination);
189+
}
182190

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

191200
if (isTestRunner) {

src/node_options.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,12 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
574574
&EnvironmentOptions::test_name_pattern);
575575
AddOption("--test-reporter",
576576
"report test output using the given reporter",
577-
&EnvironmentOptions::test_reporter);
577+
&EnvironmentOptions::test_reporter,
578+
kAllowedInEnvvar);
578579
AddOption("--test-reporter-destination",
579580
"report given reporter to the given destination",
580-
&EnvironmentOptions::test_reporter_destination);
581+
&EnvironmentOptions::test_reporter_destination,
582+
kAllowedInEnvvar);
581583
AddOption("--test-only",
582584
"run tests with 'only' option set",
583585
&EnvironmentOptions::test_only,

0 commit comments

Comments
 (0)