From e18af6ca7d756d2a99f81605e1a679db31cab5c1 Mon Sep 17 00:00:00 2001 From: Conrad Buck Date: Thu, 4 Feb 2021 15:29:58 -0500 Subject: [PATCH] Ensure verbose mode prints output synchronously Fixes #8208 --- .../jest-reporters/src/DefaultReporter.ts | 24 ++++++++++--------- .../jest-reporters/src/VerboseReporter.ts | 15 ++++++++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/packages/jest-reporters/src/DefaultReporter.ts b/packages/jest-reporters/src/DefaultReporter.ts index 77b1fbe94b46..d9aeee5b210a 100644 --- a/packages/jest-reporters/src/DefaultReporter.ts +++ b/packages/jest-reporters/src/DefaultReporter.ts @@ -43,16 +43,18 @@ export default class DefaultReporter extends BaseReporter { this._err = process.stderr.write.bind(process.stderr); this._status = new Status(); this._bufferedOutput = new Set(); - this._wrapStdio(process.stdout); - this._wrapStdio(process.stderr); + this.__wrapStdio(process.stdout); + this.__wrapStdio(process.stderr); this._status.onChange(() => { - this._clearStatus(); - this._printStatus(); + this.__clearStatus(); + this.__printStatus(); }); } - private _wrapStdio(stream: NodeJS.WritableStream | NodeJS.WriteStream) { - const originalWrite = stream.write; + protected __wrapStdio( + stream: NodeJS.WritableStream | NodeJS.WriteStream, + ): void { + const write = stream.write.bind(stream); let buffer: Array = []; let timeout: NodeJS.Timeout | null = null; @@ -62,11 +64,11 @@ export default class DefaultReporter extends BaseReporter { buffer = []; // This is to avoid conflicts between random output and status text - this._clearStatus(); + this.__clearStatus(); if (string) { - originalWrite.call(stream, string); + write(string); } - this._printStatus(); + this.__printStatus(); this._bufferedOutput.delete(flushBufferedOutput); }; @@ -103,7 +105,7 @@ export default class DefaultReporter extends BaseReporter { } } - private _clearStatus() { + protected __clearStatus(): void { if (isInteractive) { if (this._globalConfig.useStderr) { this._err(this._clear); @@ -113,7 +115,7 @@ export default class DefaultReporter extends BaseReporter { } } - private _printStatus() { + protected __printStatus(): void { const {content, clear} = this._status.get(); this._clear = clear; if (isInteractive) { diff --git a/packages/jest-reporters/src/VerboseReporter.ts b/packages/jest-reporters/src/VerboseReporter.ts index 5d7de322f330..dfe5901ef7bd 100644 --- a/packages/jest-reporters/src/VerboseReporter.ts +++ b/packages/jest-reporters/src/VerboseReporter.ts @@ -29,6 +29,21 @@ export default class VerboseReporter extends DefaultReporter { this._globalConfig = globalConfig; } + // Verbose mode is for debugging. Buffering of output is undesirable. + // See https://github.com/facebook/jest/issues/8208 + protected __wrapStdio( + stream: NodeJS.WritableStream | NodeJS.WriteStream, + ): void { + const write = stream.write.bind(stream); + + stream.write = (chunk: string) => { + this.__clearStatus(); + write(chunk); + this.__printStatus(); + return true; + }; + } + static filterTestResults( testResults: Array, ): Array {