Skip to content

Commit 6bd758f

Browse files
HinataKah0danielleadams
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: #48045 Fixes: #48011 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent f8cf353 commit 6bd758f

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
@@ -1454,6 +1454,24 @@ Emitted when all subtests have completed for a given test.
14541454

14551455
Emitted when a test starts.
14561456

1457+
### Event: `'test:stderr'`
1458+
1459+
* `data` {Object}
1460+
* `file` {string} The path of the test file.
1461+
* `message` {string} The message written to `stderr`.
1462+
1463+
Emitted when a running test writes to `stderr`.
1464+
This event is only emitted if `--test` flag is passed.
1465+
1466+
### Event: `'test:stdout'`
1467+
1468+
* `data` {Object}
1469+
* `file` {string} The path of the test file.
1470+
* `message` {string} The message written to `stdout`.
1471+
1472+
Emitted when a running test writes to `stdout`.
1473+
This event is only emitted if `--test` flag is passed.
1474+
14571475
## Class: `TestContext`
14581476

14591477
<!-- 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
@@ -284,8 +284,8 @@ class FileTest extends Test {
284284
const message = messages[i];
285285
this.addToReport({
286286
__proto__: null,
287-
type: 'test:diagnostic',
288-
data: { __proto__: null, nesting: 0, file: this.name, message },
287+
type: 'test:stdout',
288+
data: { __proto__: null, file: this.name, message },
289289
});
290290
}
291291
}
@@ -356,13 +356,12 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
356356
}
357357

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

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)