Skip to content

Commit

Permalink
Showing 3 changed files with 48 additions and 35 deletions.
37 changes: 20 additions & 17 deletions lib/testing-utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/testing-utils.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 27 additions & 17 deletions src/testing-utils.ts
@@ -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);
}
});
}

0 comments on commit 0277624

Please sign in to comment.