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 <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent d8d2d33 commit ddf819f

4 files changed

Lines changed: 36 additions & 17 deletions

File tree

‎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
returnargv;
148149
}
149150

@@ -259,7 +260,7 @@ function runTestFile(path, root, inspectPort, filesWatcher) {
259260
constsubtest=root.createSubtest(FileTest,path,async(t)=>{
260261
constargs=getRunArgs({ path, inspectPort });
261262
conststdio=['pipe','pipe','pipe'];
262-
constenv={ ...process.env};
263+
constenv={ ...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
constisTestRunner=getOptionValue('--test');
169170
constcoverage=getOptionValue('--experimental-test-coverage');
170-
constdestinations=getOptionValue('--test-reporter-destination');
171-
constreporters=getOptionValue('--test-reporter');
171+
constisChildProcess=process.env.NODE_TEST_CONTEXT==='child';
172+
letdestinations;
173+
letreporters;
172174
lettestNamePatterns;
173175
lettestOnlyFlag;
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-
thrownewERR_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+
thrownewERR_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)