test_runner: print formatted errors on summary

PR-URL: https://github.com/nodejs/node/pull/56911
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Pietro Marchini authored and Michaël Zasso committed 2025-02-10 15:16:05 +01:00
1 parent f4254b8e70
commit 0c3ae25aec
4 files changed
+111 -19

No files matched your search

+31 -19
View File
@@ -30,6 +30,31 @@ class SpecReporter extends Transform {
colors.refresh();
}
#formatFailedTestResults() {
if (this.#failedTests.length === 0) {
return '';
}
const results = [
`\n${reporterColorMap['test:fail']}${reporterUnicodeSymbolMap['test:fail']}failing tests:${colors.white}\n`,
];
for (let i = 0; i < this.#failedTests.length; i++) {
const test = this.#failedTests[i];
const formattedErr = formatTestReport('test:fail', test);
if (test.file) {
const relPath = relative(this.#cwd, test.file);
const location = `test at ${relPath}:${test.line}:${test.column}`;
ArrayPrototypePush(results, location);
}
ArrayPrototypePush(results, formattedErr);
}
this.#failedTests = []; // Clean up the failed tests
return ArrayPrototypeJoin(results, '\n'); ;
}
#handleTestReportEvent(type, data) {
const subtest = ArrayPrototypeShift(this.#stack); // This is the matching `test:start` event
if (subtest) {
@@ -74,31 +99,18 @@ class SpecReporter extends Transform {
case 'test:coverage':
return getCoverageReport(indent(data.nesting), data.summary,
reporterUnicodeSymbolMap['test:coverage'], colors.blue, true);
case 'test:summary':
// We report only the root test summary
if (data.file === undefined) {
return this.#formatFailedTestResults();
}
}
}
_transform({ type, data }, encoding, callback) {
callback(null, this.#handleEvent({ __proto__: null, type, data }));
}
_flush(callback) {
if (this.#failedTests.length === 0) {
callback(null, '');
return;
}
const results = [`\n${reporterColorMap['test:fail']}${reporterUnicodeSymbolMap['test:fail']}failing tests:${colors.white}\n`];
for (let i = 0; i < this.#failedTests.length; i++) {
const test = this.#failedTests[i];
const formattedErr = formatTestReport('test:fail', test);
if (test.file) {
const relPath = relative(this.#cwd, test.file);
const location = `test at ${relPath}:${test.line}:${test.column}`;
ArrayPrototypePush(results, location);
}
ArrayPrototypePush(results, formattedErr);
}
callback(null, ArrayPrototypeJoin(results, '\n'));
callback(null, this.#formatFailedTestResults());
}
}
@@ -0,0 +1,46 @@
import { run } from 'node:test';
import { spec } from 'node:test/reporters';
import tmpdir from '../../../common/tmpdir.js';
import { writeFileSync } from 'node:fs';
const fixtureContent = {
'dependency.js': 'module.exports = {};',
'dependency.mjs': 'export const a = 1;',
'test.js': `
const test = require('node:test');
require('./dependency.js');
import('./dependency.mjs');
import('data:text/javascript,');
test('test has ran');`,
'failing-test.js': `
const test = require('node:test');
test('failing test', () => {
throw new Error('failed');
});`,
};
tmpdir.refresh();
const fixturePaths = Object.keys(fixtureContent)
.reduce((acc, file) => ({ ...acc, [file]: tmpdir.resolve(file) }), {});
Object.entries(fixtureContent)
.forEach(([file, content]) => writeFileSync(fixturePaths[file], content));
const controller = new AbortController();
const { signal } = controller;
const stream = run({
watch: true,
cwd: tmpdir.path,
signal,
});
stream.compose(spec).pipe(process.stdout);
for await (const event of stream) {
if (event.type === 'test:watch:drained') {
controller.abort();
}
}
@@ -0,0 +1,30 @@
✖ failing test (*ms)
✔ test has ran (*ms)
ℹ tests 2
ℹ suites 0
ℹ pass 1
ℹ fail 1
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms *
✖ failing tests:
*
✖ failing test (*ms)
Error: failed
*
*
*
*
*
*
ℹ tests 0
ℹ suites 0
ℹ pass 0
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms *
+4
View File
@@ -239,6 +239,10 @@ const tests = [
name: 'test-runner/output/test-runner-plan.js',
flags: ['--test-reporter=tap'],
},
{
name: 'test-runner/output/test-runner-watch-spec.mjs',
transform: specTransform,
},
process.features.inspector ? {
name: 'test-runner/output/coverage_failure.js',
flags: ['--test-reporter=tap', '--test-coverage-exclude=!test/**'],