Skip to content

Commit

Permalink
test_runner: delegate stderr and stdout formatting to reporter
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
HinataKah0 committed May 21, 2023
1 parent 506888d commit 67f3543
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
18 changes: 18 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,24 @@ Emitted when all subtests have completed for a given test.

Emitted when a test starts.

### Event: `'test:stderr'`

* `data` {Object}
* `file` {string} The path of the test file.
* `message` {string} The message written to `stderr`.

Emitted when a running test writes to `stderr`.
This event is only emitted if `--test` flag is passed.

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

* `data` {Object}
* `file` {string} The path of the test file.
* `message` {string} The message written to `stdout`.

Emitted when a running test writes to `stdout`.
This event is only emitted if `--test` flag is passed.

## 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 @@ -117,6 +117,9 @@ class SpecReporter extends Transform {
case 'test:start':
ArrayPrototypeUnshift(this.#stack, { __proto__: null, data, type });
break;
case 'test:stderr':
case 'test:stdout':
return `${data.message}\n`;
case 'test:diagnostic':
return `${colors[type]}${this.#indent(data.nesting)}${symbols[type]}${data.message}${white}\n`;
case 'test:coverage':
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/test_runner/reporter/tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ async function * tapReporter(source) {
case 'test:start':
yield `${indent(data.nesting)}# Subtest: ${tapEscape(data.name)}\n`;
break;
case 'test:stderr':
case 'test:stdout':
case 'test:diagnostic':
yield `${indent(data.nesting)}# ${tapEscape(data.message)}\n`;
break;
Expand Down
13 changes: 6 additions & 7 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ class FileTest extends Test {
const message = messages[i];
this.addToReport({
__proto__: null,
type: 'test:diagnostic',
data: { __proto__: null, nesting: 0, file: this.name, message },
type: 'test:stdout',
data: { __proto__: null, file: this.name, message },
});
}
}
Expand Down Expand Up @@ -357,13 +357,12 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) {
}

// stderr cannot be treated as TAP, per the spec. However, we want to
// surface stderr lines as TAP diagnostics to improve the DX. Inject
// each line into the test output as an unknown token as if it came
// from the TAP parser.
// surface stderr lines to improve the DX. Inject each line into the
// test output as an unknown token as if it came from the TAP parser.
subtest.addToReport({
__proto__: null,
type: 'test:diagnostic',
data: { __proto__: null, nesting: 0, file: path, message: line },
type: 'test:stderr',
data: { __proto__: null, file: path, message: line },
});
});

Expand Down
8 changes: 8 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,14 @@ class TestsStream extends Readable {
this[kEmitMessage]('test:diagnostic', { __proto__: null, nesting, file, message });
}

stderr(file, message) {
this[kEmitMessage]('test:stderr', { __proto__: null, file, message });
}

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

coverage(nesting, file, summary) {
this[kEmitMessage]('test:coverage', { __proto__: null, nesting, file, summary });
}
Expand Down

0 comments on commit 67f3543

Please sign in to comment.