@@ -3,10 +3,29 @@ import {TestInterface} from 'ava';
33type TestContext = { stdoutWrite : any , stderrWrite : any , testOutput : string } ;
44
55function 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