From 975e38a058e810e35b64ae8c066591ed49ea472f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Mon, 18 Nov 2024 13:25:55 +0200 Subject: [PATCH] fix: restore `process.stdout/stderr` on close --- .../node/reporters/renderers/windowedRenderer.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/vitest/src/node/reporters/renderers/windowedRenderer.ts b/packages/vitest/src/node/reporters/renderers/windowedRenderer.ts index 017f9a7b14d18..d4db72b6c4e13 100644 --- a/packages/vitest/src/node/reporters/renderers/windowedRenderer.ts +++ b/packages/vitest/src/node/reporters/renderers/windowedRenderer.ts @@ -32,6 +32,7 @@ export class WindowRenderer { private windowHeight = 0 private finished = false + private cleanups: (() => void)[] = [] constructor(options: Options) { this.options = { @@ -44,8 +45,10 @@ export class WindowRenderer { error: options.logger.errorStream.write.bind(options.logger.errorStream), } - this.interceptStream(process.stdout, 'output') - this.interceptStream(process.stderr, 'error') + this.cleanups.push( + this.interceptStream(process.stdout, 'output'), + this.interceptStream(process.stderr, 'error'), + ) this.start() } @@ -57,6 +60,7 @@ export class WindowRenderer { stop() { this.write(SHOW_CURSOR, 'output') + this.cleanups.splice(0).map(fn => fn()) clearInterval(this.renderInterval) } @@ -145,6 +149,8 @@ export class WindowRenderer { } private interceptStream(stream: NodeJS.WriteStream, type: StreamType) { + const original = stream.write + // @ts-expect-error -- not sure how 2 overloads should be typed stream.write = (chunk, _, callback) => { if (chunk) { @@ -157,6 +163,10 @@ export class WindowRenderer { } callback?.() } + + return function restore() { + stream.write = original + } } private write(message: string, type: 'output' | 'error' = 'output') {