diff --git a/src/__tests__/execute-scripts/log-err.js b/src/__tests__/execute-scripts/log-err.js new file mode 100644 index 0000000..4fe2bf0 --- /dev/null +++ b/src/__tests__/execute-scripts/log-err.js @@ -0,0 +1,3 @@ +console.log("Log here"); +console.warn("Warn here"); +console.error("Error here"); diff --git a/src/__tests__/pretty-cli.js b/src/__tests__/pretty-cli.js index ba617a5..81090f2 100644 --- a/src/__tests__/pretty-cli.js +++ b/src/__tests__/pretty-cli.js @@ -27,3 +27,17 @@ test('Should escape ANSI codes properly when sliced too thin', async () => { H `) }) + +test('Should show proper stderr and stdout output', async () => { + const instance = await render('node', [ + resolve(__dirname, './execute-scripts/log-err.js'), + ]) + + await instance.findByError('Error here') + + expect(prettyCLI(instance, 300)).toMatchInlineSnapshot(` + Log here + Warn here + Error here + `) +}) diff --git a/src/config.ts b/src/config.ts index 8fd6a12..86a2ae0 100644 --- a/src/config.ts +++ b/src/config.ts @@ -29,10 +29,7 @@ let config: InternalConfig = { getInstanceError(message, testInstance: TestInstance | undefined) { let instanceWarning: string = ""; if (testInstance) { - const stdallArrStr = testInstance.stderrArr.concat(testInstance.stdoutArr) - .sort((a,b) => a.timestamp - b.timestamp) - .map(obj => obj.contents) - .join('\n'); + const stdallArrStr = testInstance.getStdallStr(); instanceWarning = `\n${stdallArrStr}` } else { instanceWarning = ""; diff --git a/src/pretty-cli.js b/src/pretty-cli.js index 7db1aa5..150c9bc 100644 --- a/src/pretty-cli.js +++ b/src/pretty-cli.js @@ -15,10 +15,7 @@ function prettyCLI(testInstance, maxLength) { throw new TypeError(`Expected an instance but got ${testInstance}`) } - const outStr = testInstance.stderrArr.concat(testInstance.stdoutArr) - .sort((a,b) => a.timestamp - b.timestamp) - .map(obj => obj.contents) - .join('\n'); + const outStr = testInstance.getStdallStr(); // eslint-disable-next-line no-negated-condition return maxLength !== undefined && outStr.length > maxLength diff --git a/src/pure.ts b/src/pure.ts index 467fd20..61cc82b 100644 --- a/src/pure.ts +++ b/src/pure.ts @@ -65,7 +65,7 @@ async function render( } const resStr = stripFinalNewline(result as string) - execOutputAPI.stdoutArr.push({contents: resStr, timestamp: Date.now()}) + execOutputAPI.stdoutArr.push({contents: resStr, timestamp: performance.now()}) _runObservers() }) @@ -76,7 +76,7 @@ async function render( } const resStr = stripFinalNewline(result as string) - execOutputAPI.stderrArr.push({contents: resStr, timestamp: Date.now()}) + execOutputAPI.stderrArr.push({contents: resStr, timestamp: performance.now()}) _runObservers() }) @@ -103,10 +103,19 @@ async function render( await execOutputAPI._isReady + + function getStdallStr(this: Omit) { + return this.stderrArr.concat(this.stdoutArr) + .sort((a,b) => a.timestamp < b.timestamp ? -1 : 1) + .map(obj => obj.contents) + .join('\n'); + } + return Object.assign( execOutputAPI, { userEvent: bindObjectFnsToInstance(execOutputAPI, userEvent), + getStdallStr: getStdallStr.bind(execOutputAPI), }, getQueriesForElement(execOutputAPI), ) as TestInstance as RenderResult diff --git a/types/pure.d.ts b/types/pure.d.ts index 75c1cac..9e41810 100644 --- a/types/pure.d.ts +++ b/types/pure.d.ts @@ -9,6 +9,7 @@ export interface TestInstance { process: ChildProcessWithoutNullStreams stdoutArr: Array<{contents: Buffer | string, timestamp: number}> stderrArr: Array<{contents: Buffer | string, timestamp: number}> + getStdallStr(): string hasExit(): null | {exitCode: number} debug(maxLength?: number): void }