Skip to content

Commit 6e503ca

Browse files
committed
test_runner: delegate stdout formatting to reporter
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`.
1 parent 506888d commit 6e503ca

File tree

8 files changed

+44
-1
lines changed

8 files changed

+44
-1
lines changed

doc/api/test.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,14 @@ Emitted when all subtests have completed for a given test.
15211521

15221522
Emitted when a test starts.
15231523

1524+
### Event: `'test:stdout'`
1525+
1526+
* `data` {Object}
1527+
* `file` {string|undefined} The path of the test file,
1528+
undefined if test is not through a file.
1529+
* `line` {string} The line outputted to `stdout`.
1530+
* `nesting` {number} The nesting level of the test.
1531+
15241532
## Class: `TestContext`
15251533

15261534
<!-- YAML

lib/internal/test_runner/reporter/spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ const colors = {
2525
'test:fail': red,
2626
'test:pass': green,
2727
'test:diagnostic': blue,
28+
'test:stdout': blue,
2829
};
2930
const symbols = {
3031
'__proto__': null,
3132
'test:fail': '\u2716 ',
3233
'test:pass': '\u2714 ',
3334
'test:diagnostic': '\u2139 ',
35+
'test:stdout': '\u2139 ',
3436
'test:coverage': '\u2139 ',
3537
'arrow:right': '\u25B6 ',
3638
'hyphen:minus': '\uFE63 ',
@@ -117,6 +119,7 @@ class SpecReporter extends Transform {
117119
case 'test:start':
118120
ArrayPrototypeUnshift(this.#stack, { __proto__: null, data, type });
119121
break;
122+
case 'test:stdout':
120123
case 'test:diagnostic':
121124
return `${colors[type]}${this.#indent(data.nesting)}${symbols[type]}${data.message}${white}\n`;
122125
case 'test:coverage':

lib/internal/test_runner/reporter/tap.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ async function * tapReporter(source) {
4545
case 'test:start':
4646
yield `${indent(data.nesting)}# Subtest: ${tapEscape(data.name)}\n`;
4747
break;
48+
case 'test:stdout':
4849
case 'test:diagnostic':
4950
yield `${indent(data.nesting)}# ${tapEscape(data.message)}\n`;
5051
break;

lib/internal/test_runner/runner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class FileTest extends Test {
285285
const message = messages[i];
286286
this.addToReport({
287287
__proto__: null,
288-
type: 'test:diagnostic',
288+
type: 'test:stdout',
289289
data: { __proto__: null, nesting: 0, file: this.name, message },
290290
});
291291
}

lib/internal/test_runner/tests_stream.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class TestsStream extends Readable {
5757
this[kEmitMessage]('test:diagnostic', { __proto__: null, nesting, file, message });
5858
}
5959

60+
stdout(nesting, file, line) {
61+
this[kEmitMessage]('test:stdout', { __proto__: null, nesting, file, line });
62+
}
63+
6064
coverage(nesting, file, summary) {
6165
this[kEmitMessage]('test:coverage', { __proto__: null, nesting, file, summary });
6266
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Flags: --no-warnings
2+
'use strict';
3+
require('../../../common');
4+
const test = require('node:test');
5+
6+
test('ok', () => {
7+
console.log('print to stdout 1');
8+
console.log('print to stdout 2');
9+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
print to stdout 1
2+
print to stdout 2
3+
TAP version 13
4+
# Subtest: ok
5+
ok 1 - ok
6+
---
7+
duration_ms: *
8+
...
9+
1..1
10+
# tests 1
11+
# suites 0
12+
# pass 1
13+
# fail 0
14+
# cancelled 0
15+
# skipped 0
16+
# todo 0
17+
# duration_ms *

test/parallel/test-runner-output.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const tests = [
4646
{ name: 'test-runner/output/unresolved_promise.js' },
4747
{ name: 'test-runner/output/default_output.js', transform: specTransform, tty: true },
4848
{ name: 'test-runner/output/arbitrary-output.js' },
49+
{ name: 'test-runner/output/stdout.js' },
4950
].map(({ name, tty, transform }) => ({
5051
name,
5152
fn: common.mustCall(async () => {

0 commit comments

Comments
 (0)