Skip to content

Commit 92bfbec

Browse files
HinataKah0Ceres6
authored andcommitted
test_runner: delegate stderr and stdout formatting to reporter
Introduce new `TestsStream` events `test:stderr` and `test:stdout` to delegate `stderr` and `stdout` (e.g. `console.log()`) formatting to the reporter. And patch existing reporters to: - Spec: output the message as it is - TAP: stay the same with existing `test:diagnostic` PR-URL: nodejs#48045 Fixes: nodejs#48011 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent ac4359d commit 92bfbec

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

doc/api/test.md

+18
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,24 @@ 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} The path of the test file.
1528+
* `message` {string} The message written to `stderr`.
1529+
1530+
Emitted when a running test writes to `stderr`.
1531+
This event is only emitted if `--test` flag is passed.
1532+
1533+
### Event: `'test:stdout'`
1534+
1535+
* `data` {Object}
1536+
* `file` {string} The path of the test file.
1537+
* `message` {string} The message written to `stdout`.
1538+
1539+
Emitted when a running test writes to `stdout`.
1540+
This event is only emitted if `--test` flag is passed.
1541+
15241542
## Class: `TestContext`
15251543

15261544
<!-- YAML

lib/internal/test_runner/reporter/spec.js

+3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ class SpecReporter extends Transform {
117117
case 'test:start':
118118
ArrayPrototypeUnshift(this.#stack, { __proto__: null, data, type });
119119
break;
120+
case 'test:stderr':
121+
case 'test:stdout':
122+
return `${data.message}\n`;
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

+2
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

+6-7
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
}
@@ -357,13 +357,12 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
357357
}
358358

359359
// stderr cannot be treated as TAP, per the spec. However, we want to
360-
// surface stderr lines as TAP diagnostics to improve the DX. Inject
361-
// each line into the test output as an unknown token as if it came
362-
// from the TAP parser.
360+
// surface stderr lines to improve the DX. Inject each line into the
361+
// test output as an unknown token as if it came from the TAP parser.
363362
subtest.addToReport({
364363
__proto__: null,
365-
type: 'test:diagnostic',
366-
data: { __proto__: null, nesting: 0, file: path, message: line },
364+
type: 'test:stderr',
365+
data: { __proto__: null, file: path, message: line },
367366
});
368367
});
369368

lib/internal/test_runner/tests_stream.js

+8
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)