Skip to content

Commit 3473edc

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 3473edc

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

doc/api/test.md

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

15221522
Emitted when a test starts.
15231523

1524+
### Event: `'test:stderr'`
1525+
1526+
* `data` {Object}
1527+
* `file` {string|undefined} The path of the test file,
1528+
undefined if test is not ran through a file.
1529+
* `message` {string} The message written to `stderr`.
1530+
1531+
Emitted when a running test writes to `stderr`.
1532+
This event is only emitted if `--test` flag is passed.
1533+
1534+
### Event: `'test:stdout'`
1535+
1536+
* `data` {Object}
1537+
* `file` {string|undefined} The path of the test file,
1538+
undefined if test is not ran through a file.
1539+
* `message` {string} The message written to `stdout`.
1540+
1541+
Emitted when a running test writes to `stdout`.
1542+
This event is only emitted if `--test` flag is passed.
1543+
15241544
## Class: `TestContext`
15251545

15261546
<!-- YAML

lib/internal/test_runner/reporter/spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ const colors = {
2525
'test:fail': red,
2626
'test:pass': green,
2727
'test:diagnostic': blue,
28+
'test:stderr': blue,
29+
'test:stdout': blue,
2830
};
2931
const symbols = {
3032
'__proto__': null,
3133
'test:fail': '\u2716 ',
3234
'test:pass': '\u2714 ',
3335
'test:diagnostic': '\u2139 ',
36+
'test:stderr': '\u2139 ',
37+
'test:stdout': '\u2139 ',
3438
'test:coverage': '\u2139 ',
3539
'arrow:right': '\u25B6 ',
3640
'hyphen:minus': '\uFE63 ',
@@ -117,6 +121,8 @@ class SpecReporter extends Transform {
117121
case 'test:start':
118122
ArrayPrototypeUnshift(this.#stack, { __proto__: null, data, type });
119123
break;
124+
case 'test:stderr':
125+
case 'test:stdout':
120126
case 'test:diagnostic':
121127
return `${colors[type]}${this.#indent(data.nesting)}${symbols[type]}${data.message}${white}\n`;
122128
case 'test:coverage':

lib/internal/test_runner/reporter/tap.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ async function * tapReporter(source) {
4545
case 'test:start':
4646
yield `${indent(data.nesting)}# Subtest: ${tapEscape(data.name)}\n`;
4747
break;
48+
case 'test:stderr':
49+
case 'test:stdout':
4850
case 'test:diagnostic':
4951
yield `${indent(data.nesting)}# ${tapEscape(data.message)}\n`;
5052
break;

lib/internal/test_runner/runner.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ class FileTest extends Test {
285285
const message = messages[i];
286286
this.addToReport({
287287
__proto__: null,
288-
type: 'test:diagnostic',
289-
data: { __proto__: null, nesting: 0, file: this.name, message },
288+
type: 'test:stdout',
289+
data: { __proto__: null, file: this.name, message },
290290
});
291291
}
292292
}
@@ -362,8 +362,8 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
362362
// from the TAP parser.
363363
subtest.addToReport({
364364
__proto__: null,
365-
type: 'test:diagnostic',
366-
data: { __proto__: null, nesting: 0, file: path, message: line },
365+
type: 'test:stderr',
366+
data: { __proto__: null, file: path, message: line },
367367
});
368368
});
369369

lib/internal/test_runner/tests_stream.js

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

60+
stderr(file, message) {
61+
this[kEmitMessage]('test:stderr', { __proto__: null, file, message });
62+
}
63+
64+
stdout(file, message) {
65+
this[kEmitMessage]('test:stdout', { __proto__: null, file, message });
66+
}
67+
6068
coverage(nesting, file, summary) {
6169
this[kEmitMessage]('test:coverage', { __proto__: null, nesting, file, summary });
6270
}

0 commit comments

Comments
 (0)