Only print test output for failing tests

A spec reporter subclass buffers console output, including winston's
ConsoleTransport and the integration child process lines, and prints it
above the failure line when a test or hook fails. VERBOSE_TEST streams
everything as before, and CI sets it on debug re-runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5.5 committed 2026-11-17 12:27:29 +00:00
1 parent 5cf14eb15b
commit 838a43428b
4 files changed
+47 -1

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
// Spec reporter that holds back console output and prints it only for
// failing tests and hooks. Set VERBOSE_TEST to print everything as it happens.
"use strict";
const util = require("node:util");
const Mocha = require("mocha");
const { Spec } = Mocha.reporters;
const { EVENT_TEST_FAIL, EVENT_TEST_PASS, EVENT_TEST_PENDING } = Mocha.Runner.constants;
const methods = ["log", "info", "debug", "warn", "error"];
class CaptureReporter extends Spec {
constructor(runner, options) {
super(runner, options);
if (process.env.VERBOSE_TEST) {
return;
}
const original = {};
const captured = [];
for (const method of methods) {
original[method] = console[method];
console[method] = (...args) => { captured.push([method, util.format(...args)]); };
}
const discard = () => { captured.length = 0; };
runner.on(EVENT_TEST_PASS, discard);
runner.on(EVENT_TEST_PENDING, discard);
// Prepend so the output lands above the failure line.
runner.prependListener(EVENT_TEST_FAIL, () => {
for (const [method, text] of captured) {
original[method](text);
}
discard();
});
}
}
module.exports = CaptureReporter;