Skip to content

Commit

Permalink
Showing 3 changed files with 45 additions and 9 deletions.
23 changes: 20 additions & 3 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.

29 changes: 24 additions & 5 deletions src/testing-utils.ts
@@ -3,10 +3,29 @@ 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;
// Function signature taken from Socket.write.
// Note there are two overloads:
// write(buffer: Uint8Array | string, cb?: (err?: Error) => void): boolean;
// write(str: Uint8Array | string, encoding?: string, cb?: (err?: Error) => void): boolean;
return (chunk: Uint8Array | string, encoding?: string, cb?: (err?: Error) => void): boolean => {
// Work out which method overload we are in
if (cb === undefined && typeof encoding === 'function') {
cb = encoding;
encoding = undefined;
}

// Record the output
if (typeof chunk === 'string') {
context.testOutput += chunk;
} else {
context.testOutput += new TextDecoder(encoding || 'utf-8').decode(chunk);
}

// Satisfy contract by calling callback when done
if (cb !== undefined && typeof cb === 'function') {
cb();
}

return true;
};
}
@@ -19,11 +38,11 @@ export function silenceDebugOutput(test: TestInterface<any>) {

const processStdoutWrite = process.stdout.write.bind(process.stdout);
t.context.stdoutWrite = processStdoutWrite;
process.stdout.write = wrapOutput(t.context);
process.stdout.write = wrapOutput(t.context) as any;

const processStderrWrite = process.stderr.write.bind(process.stderr);
t.context.stderrWrite = processStderrWrite;
process.stderr.write = wrapOutput(t.context);
process.stderr.write = wrapOutput(t.context) as any;
});

typedTest.afterEach.always(t => {

0 comments on commit 52e5243

Please sign in to comment.