Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: print formatted errors on summary #56911

Merged
merged 2 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions lib/internal/test_runner/reporter/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
cjihrig marked this conversation as resolved.
Show resolved Hide resolved
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());
pmarchini marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
46 changes: 46 additions & 0 deletions test/fixtures/test-runner/output/test-runner-watch-spec.mjs
Original file line number Diff line number Diff line change
@@ -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();
}
}
30 changes: 30 additions & 0 deletions test/fixtures/test-runner/output/test-runner-watch-spec.snapshot
Original file line number Diff line number Diff line change
@@ -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
cjihrig marked this conversation as resolved.
Show resolved Hide resolved
ℹ duration_ms *
4 changes: 4 additions & 0 deletions test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,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/**'],
Expand Down
Loading