From 52e52435f75b99055a91c23ed6489f41cc43ea3c Mon Sep 17 00:00:00 2001 From: Robert Brignull Date: Tue, 23 Jun 2020 17:17:11 +0100 Subject: [PATCH] handle Uint8Array --- lib/testing-utils.js | 23 ++++++++++++++++++++--- lib/testing-utils.js.map | 2 +- src/testing-utils.ts | 29 ++++++++++++++++++++++++----- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/lib/testing-utils.js b/lib/testing-utils.js index e1659282a..32d9f6142 100644 --- a/lib/testing-utils.js +++ b/lib/testing-utils.js @@ -1,9 +1,26 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function wrapOutput(context) { - return (str) => { - 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, encoding, cb) => { + // 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; }; diff --git a/lib/testing-utils.js.map b/lib/testing-utils.js.map index a27fa46cb..cc691d2ba 100644 --- a/lib/testing-utils.js.map +++ b/lib/testing-utils.js.map @@ -1 +1 @@ -{"version":3,"file":"testing-utils.js","sourceRoot":"","sources":["../src/testing-utils.ts"],"names":[],"mappings":";;AAIA,SAAS,UAAU,CAAC,OAAoB;IACtC,OAAO,CAAC,GAAQ,EAAW,EAAE;QAC3B,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YAC3B,OAAO,CAAC,UAAU,IAAI,GAAG,CAAC;SAC3B;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;AACJ,CAAC;AAED,SAAgB,kBAAkB,CAAC,IAAwB;IACzD,MAA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AAC,CAAC,CAAC,OAAO,CAAQ,CAAC;IACxD,CAAC,CAAC,CAAC;IAEH,SAAS,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE;QAC3B,OAAO,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC;QAC7C,OAAO,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC;QAE7C,IAAI,CAAC,CAAC,CAAC,MAAM,EAAE;YACb,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;SAC5C;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAvBD,gDAuBC"} \ No newline at end of file diff --git a/src/testing-utils.ts b/src/testing-utils.ts index 2a72b6034..7334ca1a5 100644 --- a/src/testing-utils.ts +++ b/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) { 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 => {