Skip to content

Commit

Permalink
test_runner: delegate stdout formatting to reporter
Browse files Browse the repository at this point in the history
Introduce new `TestsStream` event `test:stdout` to delegate
stdout (e.g. `console.log()`) formatting to the reporter.
And patch existing reporters to treat `test:stdout` similar
with `test:diagnostic`.
  • Loading branch information
HinataKah0 committed May 18, 2023
1 parent 506888d commit 3473edc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
20 changes: 20 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,26 @@ Emitted when all subtests have completed for a given test.

Emitted when a test starts.

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

* `data` {Object}
* `file` {string|undefined} The path of the test file,
undefined if test is not ran through a file.
* `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'`

* `data` {Object}
* `file` {string|undefined} The path of the test file,
undefined if test is not ran through a 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
6 changes: 6 additions & 0 deletions lib/internal/test_runner/reporter/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ const colors = {
'test:fail': red,
'test:pass': green,
'test:diagnostic': blue,
'test:stderr': blue,
'test:stdout': blue,
};
const symbols = {
'__proto__': null,
'test:fail': '\u2716 ',
'test:pass': '\u2714 ',
'test:diagnostic': '\u2139 ',
'test:stderr': '\u2139 ',
'test:stdout': '\u2139 ',
'test:coverage': '\u2139 ',
'arrow:right': '\u25B6 ',
'hyphen:minus': '\uFE63 ',
Expand Down Expand Up @@ -117,6 +121,8 @@ class SpecReporter extends Transform {
case 'test:start':
ArrayPrototypeUnshift(this.#stack, { __proto__: null, data, type });
break;
case 'test:stderr':
case 'test:stdout':
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
8 changes: 4 additions & 4 deletions lib/internal/test_runner/runner.js
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 @@ -362,8 +362,8 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
// 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

0 comments on commit 3473edc

Please sign in to comment.