Skip to content

Commit 0277624

Browse files
Only output on failing tests
1 parent 66be268 commit 0277624

File tree

3 files changed

+48
-35
lines changed

3 files changed

+48
-35
lines changed

lib/testing-utils.js

Lines changed: 20 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/testing-utils.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/testing-utils.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
import {TestInterface} from 'ava';
22

3+
type TestContext = {stdoutWrite: any, stderrWrite: any, testOutput: string};
4+
5+
function wrapOutput(context: TestContext) {
6+
return (str: any): boolean => {
7+
if (typeof str === 'string') {
8+
context.testOutput += str;
9+
}
10+
return true;
11+
}
12+
}
13+
314
export function silenceDebugOutput(test: TestInterface<any>) {
4-
const typedTest = test as TestInterface<{write: any}>;
15+
const typedTest = test as TestInterface<TestContext>;
516

617
typedTest.beforeEach(t => {
18+
t.context.testOutput = "";
19+
720
const processStdoutWrite = process.stdout.write.bind(process.stdout);
8-
t.context.write = processStdoutWrite;
9-
process.stdout.write = (str: Uint8Array | string, encoding?: any, cb?: (err?: Error) => void) => {
10-
// Core library will directly call process.stdout.write for commands
11-
// We don't want debug output to be included in tests
12-
if (typeof str === "string") {
13-
str = str.replace(/::(info|debug|warning).*/, '');
14-
if (str.trim() !== "") {
15-
processStdoutWrite(str, encoding, cb);
16-
}
17-
} else {
18-
processStdoutWrite(str, encoding, cb);
19-
}
20-
return true;
21-
};
21+
t.context.stdoutWrite = processStdoutWrite;
22+
process.stdout.write = wrapOutput(t.context);
23+
24+
const processStderrWrite = process.stderr.write.bind(process.stderr);
25+
t.context.stderrWrite = processStderrWrite;
26+
process.stderr.write = wrapOutput(t.context);
2227
});
2328

24-
typedTest.afterEach(t => {
25-
process.stdout.write = t.context.write;
29+
typedTest.afterEach.always(t => {
30+
process.stdout.write = t.context.stdoutWrite;
31+
process.stderr.write = t.context.stderrWrite;
32+
33+
if (!t.passed) {
34+
process.stdout.write(t.context.testOutput);
35+
}
2636
});
2737
}

0 commit comments

Comments
 (0)