Only output on failing tests

This commit is contained in:
Robert Brignull 2020-06-23 14:33:20 +01:00
parent 66be268a09
commit 02776246bf
3 changed files with 48 additions and 35 deletions

View file

@ -1,27 +1,37 @@
import {TestInterface} from 'ava';
type TestContext = {stdoutWrite: any, stderrWrite: any, testOutput: string};
function wrapOutput(context: TestContext) {
return (str: any): boolean => {
if (typeof str === 'string') {
context.testOutput += str;
}
return true;
}
}
export function silenceDebugOutput(test: TestInterface<any>) {
const typedTest = test as TestInterface<{write: any}>;
const typedTest = test as TestInterface<TestContext>;
typedTest.beforeEach(t => {
t.context.testOutput = "";
const processStdoutWrite = process.stdout.write.bind(process.stdout);
t.context.write = processStdoutWrite;
process.stdout.write = (str: Uint8Array | string, encoding?: any, cb?: (err?: Error) => void) => {
// Core library will directly call process.stdout.write for commands
// We don't want debug output to be included in tests
if (typeof str === "string") {
str = str.replace(/::(info|debug|warning).*/, '');
if (str.trim() !== "") {
processStdoutWrite(str, encoding, cb);
}
} else {
processStdoutWrite(str, encoding, cb);
}
return true;
};
t.context.stdoutWrite = processStdoutWrite;
process.stdout.write = wrapOutput(t.context);
const processStderrWrite = process.stderr.write.bind(process.stderr);
t.context.stderrWrite = processStderrWrite;
process.stderr.write = wrapOutput(t.context);
});
typedTest.afterEach(t => {
process.stdout.write = t.context.write;
typedTest.afterEach.always(t => {
process.stdout.write = t.context.stdoutWrite;
process.stderr.write = t.context.stderrWrite;
if (!t.passed) {
process.stdout.write(t.context.testOutput);
}
});
}