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 17, 2023
1 parent 506888d commit 6e503ca
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 1 deletion.
8 changes: 8 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,14 @@ Emitted when all subtests have completed for a given test.

Emitted when a test starts.

### Event: `'test:stdout'`

* `data` {Object}
* `file` {string|undefined} The path of the test file,
undefined if test is not through a file.
* `line` {string} The line outputted to `stdout`.
* `nesting` {number} The nesting level of the test.

## 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 @@ -25,12 +25,14 @@ const colors = {
'test:fail': red,
'test:pass': green,
'test:diagnostic': blue,
'test:stdout': blue,
};
const symbols = {
'__proto__': null,
'test:fail': '\u2716 ',
'test:pass': '\u2714 ',
'test:diagnostic': '\u2139 ',
'test:stdout': '\u2139 ',
'test:coverage': '\u2139 ',
'arrow:right': '\u25B6 ',
'hyphen:minus': '\uFE63 ',
Expand Down Expand Up @@ -117,6 +119,7 @@ class SpecReporter extends Transform {
case 'test:start':
ArrayPrototypeUnshift(this.#stack, { __proto__: null, data, type });
break;
case 'test:stdout':
case 'test:diagnostic':
return `${colors[type]}${this.#indent(data.nesting)}${symbols[type]}${data.message}${white}\n`;
case 'test:coverage':
Expand Down
1 change: 1 addition & 0 deletions lib/internal/test_runner/reporter/tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async function * tapReporter(source) {
case 'test:start':
yield `${indent(data.nesting)}# Subtest: ${tapEscape(data.name)}\n`;
break;
case 'test:stdout':
case 'test:diagnostic':
yield `${indent(data.nesting)}# ${tapEscape(data.message)}\n`;
break;
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class FileTest extends Test {
const message = messages[i];
this.addToReport({
__proto__: null,
type: 'test:diagnostic',
type: 'test:stdout',
data: { __proto__: null, nesting: 0, file: this.name, message },
});
}
Expand Down
4 changes: 4 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,10 @@ class TestsStream extends Readable {
this[kEmitMessage]('test:diagnostic', { __proto__: null, nesting, file, message });
}

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

coverage(nesting, file, summary) {
this[kEmitMessage]('test:coverage', { __proto__: null, nesting, file, summary });
}
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/test-runner/output/stdout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Flags: --no-warnings
'use strict';
require('../../../common');
const test = require('node:test');

test('ok', () => {
console.log('print to stdout 1');
console.log('print to stdout 2');
});
17 changes: 17 additions & 0 deletions test/fixtures/test-runner/output/stdout.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
print to stdout 1
print to stdout 2
TAP version 13
# Subtest: ok
ok 1 - ok
---
duration_ms: *
...
1..1
# tests 1
# suites 0
# pass 1
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms *
1 change: 1 addition & 0 deletions test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const tests = [
{ name: 'test-runner/output/unresolved_promise.js' },
{ name: 'test-runner/output/default_output.js', transform: specTransform, tty: true },
{ name: 'test-runner/output/arbitrary-output.js' },
{ name: 'test-runner/output/stdout.js' },
].map(({ name, tty, transform }) => ({
name,
fn: common.mustCall(async () => {
Expand Down

0 comments on commit 6e503ca

Please sign in to comment.