Skip to content

Commit 15803a0

Browse files
committed
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: nodejs#46484
1 parent 9e06ef0 commit 15803a0

File tree

5 files changed

+40
-16
lines changed

5 files changed

+40
-16
lines changed

doc/api/cli.md

+8
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,12 @@ added: v19.6.0
12721272
The destination for the corresponding test reporter. See the documentation on
12731273
[test reporters][] for more details.
12741274

1275+
### `--test-child-process`
1276+
1277+
A flag to identify the process as a child of another test process to ensure
1278+
that test reporting is formatted correctly to be parsed by a parent test
1279+
process.
1280+
12751281
### `--test-only`
12761282

12771283
<!-- YAML
@@ -1952,6 +1958,8 @@ Node.js options that are allowed are:
19521958
* `--secure-heap`
19531959
* `--snapshot-blob`
19541960
* `--test-only`
1961+
* `--test-reporter-destination`
1962+
* `--test-reporter`
19551963
* `--throw-deprecation`
19561964
* `--title`
19571965
* `--tls-cipher-list`

lib/internal/test_runner/runner.js

+5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const {
44
ArrayPrototypeFilter,
55
ArrayPrototypeForEach,
66
ArrayPrototypeIncludes,
7+
ArrayPrototypeJoin,
78
ArrayPrototypePush,
89
ArrayPrototypeSlice,
910
ArrayPrototypeSome,
1011
ArrayPrototypeSort,
12+
ArrayPrototypeUnshift,
1113
FunctionPrototypeCall,
1214
Number,
1315
ObjectAssign,
@@ -21,6 +23,7 @@ const {
2123
SafeSet,
2224
StringPrototypeIndexOf,
2325
StringPrototypeSlice,
26+
StringPrototypeSplit,
2427
StringPrototypeStartsWith,
2528
} = primordials;
2629

@@ -143,6 +146,8 @@ function getRunArgs({ path, inspectPort }) {
143146
ArrayPrototypePush(argv, `--inspect-port=${getInspectPort(inspectPort)}`);
144147
}
145148
ArrayPrototypePush(argv, path);
149+
ArrayPrototypeUnshift(argv, '--test-child-process');
150+
146151
return argv;
147152
}
148153

lib/internal/test_runner/utils.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -166,25 +166,31 @@ function parseCommandLine() {
166166

167167
const isTestRunner = getOptionValue('--test');
168168
const coverage = getOptionValue('--experimental-test-coverage');
169-
const destinations = getOptionValue('--test-reporter-destination');
170-
const reporters = getOptionValue('--test-reporter');
169+
let destinations = getOptionValue('--test-reporter-destination');
170+
let reporters = getOptionValue('--test-reporter');
171+
const isChildProcess = getOptionValue('--test-child-process');
171172
let testNamePatterns;
172173
let testOnlyFlag;
173174

174-
if (reporters.length === 0 && destinations.length === 0) {
175-
ArrayPrototypePush(reporters, kDefaultReporter);
176-
}
175+
if (isChildProcess) {
176+
reporters = [kDefaultReporter];
177+
destinations = [kDefaultDestination];
178+
} else {
179+
if (reporters.length === 0 && destinations.length === 0) {
180+
ArrayPrototypePush(reporters, kDefaultReporter);
181+
}
177182

178-
if (reporters.length === 1 && destinations.length === 0) {
179-
ArrayPrototypePush(destinations, kDefaultDestination);
180-
}
183+
if (reporters.length === 1 && destinations.length === 0) {
184+
ArrayPrototypePush(destinations, kDefaultDestination);
185+
}
181186

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-
);
187+
if (destinations.length !== reporters.length) {
188+
throw new ERR_INVALID_ARG_VALUE(
189+
'--test-reporter',
190+
reporters,
191+
'must match the number of specified \'--test-reporter-destination\'',
192+
);
193+
}
188194
}
189195

190196
if (isTestRunner) {

src/node_options.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,14 @@ 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);
578+
AddOption("--test-child-process", "", // for internal use by test runner
579+
&EnvironmentOptions::test_child_process);
576580
AddOption("--test-only",
577581
"run tests with 'only' option set",
578582
&EnvironmentOptions::test_only,

src/node_options.h

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class EnvironmentOptions : public Options {
158158
std::vector<std::string> test_name_pattern;
159159
std::vector<std::string> test_reporter;
160160
std::vector<std::string> test_reporter_destination;
161+
bool test_child_process = false;
161162
bool test_only = false;
162163
bool test_udp_no_try_send = false;
163164
bool throw_deprecation = false;

0 commit comments

Comments
 (0)