Skip to content

Commit

Permalink
fix: restore process.stdout/stderr on close
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Nov 18, 2024
1 parent 7b56195 commit 975e38a
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/vitest/src/node/reporters/renderers/windowedRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class WindowRenderer {

private windowHeight = 0
private finished = false
private cleanups: (() => void)[] = []

constructor(options: Options) {
this.options = {
Expand All @@ -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()
}
Expand All @@ -57,6 +60,7 @@ export class WindowRenderer {

stop() {
this.write(SHOW_CURSOR, 'output')
this.cleanups.splice(0).map(fn => fn())
clearInterval(this.renderInterval)
}

Expand Down Expand Up @@ -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) {
Expand All @@ -157,6 +163,10 @@ export class WindowRenderer {
}
callback?.()
}

return function restore() {
stream.write = original
}
}

private write(message: string, type: 'output' | 'error' = 'output') {
Expand Down

0 comments on commit 975e38a

Please sign in to comment.