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 a49a11b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
"expect-type": "^1.1.0",
"magic-string": "^0.30.12",
"pathe": "^1.1.2",
"restore-cursor": "^5.1.0",
"std-env": "^3.8.0",
"tinybench": "^2.9.0",
"tinyexec": "^0.3.1",
Expand Down
19 changes: 16 additions & 3 deletions packages/vitest/src/node/reporters/renderers/windowedRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Writable } from 'node:stream'
import type { Vitest } from '../../core'
import { stripVTControlCharacters } from 'node:util'
import restoreCursor from 'restore-cursor'

const DEFAULT_RENDER_INTERVAL = 16

Expand Down Expand Up @@ -32,6 +33,7 @@ export class WindowRenderer {

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

constructor(options: Options) {
this.options = {
Expand All @@ -44,8 +46,13 @@ 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'),
)

restoreCursor()
this.write(HIDE_CURSOR, 'output')

this.start()
}
Expand All @@ -57,6 +64,7 @@ export class WindowRenderer {

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

Expand Down Expand Up @@ -125,7 +133,6 @@ export class WindowRenderer {

this.write(windowContent.join('\n'))
this.write(SYNC_END)
this.write(HIDE_CURSOR)

this.windowHeight = rowCount + Math.max(0, padding)
}
Expand All @@ -145,6 +152,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 +166,10 @@ export class WindowRenderer {
}
callback?.()
}

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

private write(message: string, type: 'output' | 'error' = 'output') {
Expand Down
26 changes: 26 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a49a11b

Please sign in to comment.