Skip to content

Commit

Permalink
test_runner: support defining test reporter in NODE_OPTIONS
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SRHerzog committed Feb 23, 2023
1 parent 9e06ef0 commit 15803a0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
8 changes: 8 additions & 0 deletions doc/api/cli.md
Expand Up @@ -1272,6 +1272,12 @@ added: v19.6.0
The destination for the corresponding test reporter. See the documentation on
[test reporters][] for more details.

### `--test-child-process`

A flag to identify the process as a child of another test process to ensure
that test reporting is formatted correctly to be parsed by a parent test
process.

### `--test-only`

<!-- YAML
Expand Down Expand Up @@ -1952,6 +1958,8 @@ Node.js options that are allowed are:
* `--secure-heap`
* `--snapshot-blob`
* `--test-only`
* `--test-reporter-destination`
* `--test-reporter`
* `--throw-deprecation`
* `--title`
* `--tls-cipher-list`
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/test_runner/runner.js
Expand Up @@ -4,10 +4,12 @@ const {
ArrayPrototypeFilter,
ArrayPrototypeForEach,
ArrayPrototypeIncludes,
ArrayPrototypeJoin,
ArrayPrototypePush,
ArrayPrototypeSlice,
ArrayPrototypeSome,
ArrayPrototypeSort,
ArrayPrototypeUnshift,
FunctionPrototypeCall,
Number,
ObjectAssign,
Expand All @@ -21,6 +23,7 @@ const {
SafeSet,
StringPrototypeIndexOf,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
} = primordials;

Expand Down Expand Up @@ -143,6 +146,8 @@ function getRunArgs({ path, inspectPort }) {
ArrayPrototypePush(argv, `--inspect-port=${getInspectPort(inspectPort)}`);
}
ArrayPrototypePush(argv, path);
ArrayPrototypeUnshift(argv, '--test-child-process');

return argv;
}

Expand Down
34 changes: 20 additions & 14 deletions lib/internal/test_runner/utils.js
Expand Up @@ -166,25 +166,31 @@ function parseCommandLine() {

const isTestRunner = getOptionValue('--test');
const coverage = getOptionValue('--experimental-test-coverage');
const destinations = getOptionValue('--test-reporter-destination');
const reporters = getOptionValue('--test-reporter');
let destinations = getOptionValue('--test-reporter-destination');
let reporters = getOptionValue('--test-reporter');
const isChildProcess = getOptionValue('--test-child-process');
let testNamePatterns;
let testOnlyFlag;

if (reporters.length === 0 && destinations.length === 0) {
ArrayPrototypePush(reporters, kDefaultReporter);
}
if (isChildProcess) {
reporters = [kDefaultReporter];
destinations = [kDefaultDestination];
} else {
if (reporters.length === 0 && destinations.length === 0) {
ArrayPrototypePush(reporters, kDefaultReporter);
}

if (reporters.length === 1 && destinations.length === 0) {
ArrayPrototypePush(destinations, kDefaultDestination);
}
if (reporters.length === 1 && destinations.length === 0) {
ArrayPrototypePush(destinations, kDefaultDestination);
}

if (destinations.length !== reporters.length) {
throw new ERR_INVALID_ARG_VALUE(
'--test-reporter',
reporters,
'must match the number of specified \'--test-reporter-destination\'',
);
if (destinations.length !== reporters.length) {
throw new ERR_INVALID_ARG_VALUE(
'--test-reporter',
reporters,
'must match the number of specified \'--test-reporter-destination\'',
);
}
}

if (isTestRunner) {
Expand Down
8 changes: 6 additions & 2 deletions src/node_options.cc
Expand Up @@ -569,10 +569,14 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
&EnvironmentOptions::test_name_pattern);
AddOption("--test-reporter",
"report test output using the given reporter",
&EnvironmentOptions::test_reporter);
&EnvironmentOptions::test_reporter,
kAllowedInEnvvar);
AddOption("--test-reporter-destination",
"report given reporter to the given destination",
&EnvironmentOptions::test_reporter_destination);
&EnvironmentOptions::test_reporter_destination,
kAllowedInEnvvar);
AddOption("--test-child-process", "", // for internal use by test runner
&EnvironmentOptions::test_child_process);
AddOption("--test-only",
"run tests with 'only' option set",
&EnvironmentOptions::test_only,
Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Expand Up @@ -158,6 +158,7 @@ class EnvironmentOptions : public Options {
std::vector<std::string> test_name_pattern;
std::vector<std::string> test_reporter;
std::vector<std::string> test_reporter_destination;
bool test_child_process = false;
bool test_only = false;
bool test_udp_no_try_send = false;
bool throw_deprecation = false;
Expand Down

0 comments on commit 15803a0

Please sign in to comment.