Skip to content

Commit 52e5243

Browse files
handle Uint8Array
1 parent 350bf48 commit 52e5243

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

lib/testing-utils.js

Lines changed: 20 additions & 3 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: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,29 @@ import {TestInterface} from 'ava';
33
type TestContext = {stdoutWrite: any, stderrWrite: any, testOutput: string};
44

55
function wrapOutput(context: TestContext) {
6-
return (str: any): boolean => {
7-
if (typeof str === 'string') {
8-
context.testOutput += str;
6+
// Function signature taken from Socket.write.
7+
// Note there are two overloads:
8+
// write(buffer: Uint8Array | string, cb?: (err?: Error) => void): boolean;
9+
// write(str: Uint8Array | string, encoding?: string, cb?: (err?: Error) => void): boolean;
10+
return (chunk: Uint8Array | string, encoding?: string, cb?: (err?: Error) => void): boolean => {
11+
// Work out which method overload we are in
12+
if (cb === undefined && typeof encoding === 'function') {
13+
cb = encoding;
14+
encoding = undefined;
915
}
16+
17+
// Record the output
18+
if (typeof chunk === 'string') {
19+
context.testOutput += chunk;
20+
} else {
21+
context.testOutput += new TextDecoder(encoding || 'utf-8').decode(chunk);
22+
}
23+
24+
// Satisfy contract by calling callback when done
25+
if (cb !== undefined && typeof cb === 'function') {
26+
cb();
27+
}
28+
1029
return true;
1130
};
1231
}
@@ -19,11 +38,11 @@ export function silenceDebugOutput(test: TestInterface<any>) {
1938

2039
const processStdoutWrite = process.stdout.write.bind(process.stdout);
2140
t.context.stdoutWrite = processStdoutWrite;
22-
process.stdout.write = wrapOutput(t.context);
41+
process.stdout.write = wrapOutput(t.context) as any;
2342

2443
const processStderrWrite = process.stderr.write.bind(process.stderr);
2544
t.context.stderrWrite = processStderrWrite;
26-
process.stderr.write = wrapOutput(t.context);
45+
process.stderr.write = wrapOutput(t.context) as any;
2746
});
2847

2948
typedTest.afterEach.always(t => {

0 commit comments

Comments
 (0)