Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: delegate stderr and stdout formatting to reporter #48045

Merged
merged 1 commit into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,24 @@ Emitted when all subtests have completed for a given test.

Emitted when a test starts.

### Event: `'test:stderr'`

* `data` {Object}
* `file` {string} The path of the test file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the test:diagnostic event, this can also be undefined. Is that possible for these new events?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. test:diagnostic can be produced inside the REPL. these new events are only useful with --test

* `message` {string} The message written to `stderr`.

Emitted when a running test writes to `stderr`.
This event is only emitted if `--test` flag is passed.

### Event: `'test:stdout'`
MoLow marked this conversation as resolved.
Show resolved Hide resolved
MoLow marked this conversation as resolved.
Show resolved Hide resolved

* `data` {Object}
* `file` {string} The path of the test file.
* `message` {string} The message written to `stdout`.

Emitted when a running test writes to `stdout`.
This event is only emitted if `--test` flag is passed.

## Class: `TestContext`

<!-- YAML
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/test_runner/reporter/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ class SpecReporter extends Transform {
case 'test:start':
ArrayPrototypeUnshift(this.#stack, { __proto__: null, data, type });
break;
case 'test:stderr':
case 'test:stdout':
return `${data.message}\n`;
case 'test:diagnostic':
return `${colors[type]}${this.#indent(data.nesting)}${symbols[type]}${data.message}${white}\n`;
case 'test:coverage':
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/test_runner/reporter/tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ async function * tapReporter(source) {
case 'test:start':
yield `${indent(data.nesting)}# Subtest: ${tapEscape(data.name)}\n`;
break;
case 'test:stderr':
case 'test:stdout':
case 'test:diagnostic':
yield `${indent(data.nesting)}# ${tapEscape(data.message)}\n`;
break;
Expand Down
13 changes: 6 additions & 7 deletions lib/internal/test_runner/runner.js
MoLow marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ class FileTest extends Test {
const message = messages[i];
this.addToReport({
__proto__: null,
type: 'test:diagnostic',
data: { __proto__: null, nesting: 0, file: this.name, message },
type: 'test:stdout',
data: { __proto__: null, file: this.name, message },
});
}
}
Expand Down Expand Up @@ -357,13 +357,12 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
}

// stderr cannot be treated as TAP, per the spec. However, we want to
// surface stderr lines as TAP diagnostics to improve the DX. Inject
// each line into the test output as an unknown token as if it came
// from the TAP parser.
// surface stderr lines to improve the DX. Inject each line into the
// test output as an unknown token as if it came from the TAP parser.
subtest.addToReport({
__proto__: null,
type: 'test:diagnostic',
data: { __proto__: null, nesting: 0, file: path, message: line },
type: 'test:stderr',
data: { __proto__: null, file: path, message: line },
});
});

Expand Down
8 changes: 8 additions & 0 deletions lib/internal/test_runner/tests_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class TestsStream extends Readable {
this[kEmitMessage]('test:diagnostic', { __proto__: null, nesting, file, message });
}

stderr(file, message) {
this[kEmitMessage]('test:stderr', { __proto__: null, file, message });
}

stdout(file, message) {
this[kEmitMessage]('test:stdout', { __proto__: null, file, message });
}

coverage(nesting, file, summary) {
this[kEmitMessage]('test:coverage', { __proto__: null, nesting, file, summary });
}
Expand Down