Skip to content

Commit

Permalink
fix(reporters): write buffered stdout/stderr on process exit (#6932)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Nov 26, 2024
1 parent a5bbc0a commit 80cde2a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 54 deletions.
24 changes: 0 additions & 24 deletions packages/vitest/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1245,30 +1245,6 @@ Repository: git://github.com/feross/run-parallel.git
---------------------------------------

## signal-exit
License: ISC
By: Ben Coe
Repository: https://github.com/tapjs/signal-exit.git

> The ISC License
>
> Copyright (c) 2015, Contributors
>
> Permission to use, copy, modify, and/or distribute this software
> for any purpose with or without fee is hereby granted, provided
> that the above copyright notice and this permission notice
> appear in all copies.
>
> THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
> WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
> OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE
> LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
> OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
> WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
> ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
---------------------------------------

## sisteransi
License: MIT
By: Terkel Gjervig
Expand Down
1 change: 0 additions & 1 deletion packages/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@
"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
2 changes: 1 addition & 1 deletion packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export abstract class BaseReporter implements Reporter {
}

else if (this.renderSucceed || anyFailed) {
this.log(` ${c.green(c.dim(F_CHECK))} ${getTestName(test, c.dim(' > '))}`)
this.log(` ${c.dim(getStateSymbol(test))} ${getTestName(test, c.dim(' > '))}`)
}
}
}
Expand Down
29 changes: 27 additions & 2 deletions packages/vitest/src/node/reporters/renderers/windowedRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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 @@ -49,9 +48,9 @@ export class WindowRenderer {
this.cleanups.push(
this.interceptStream(process.stdout, 'output'),
this.interceptStream(process.stderr, 'error'),
this.addProcessExitListeners(),
)

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

this.start()
Expand Down Expand Up @@ -175,6 +174,32 @@ export class WindowRenderer {
private write(message: string, type: 'output' | 'error' = 'output') {
(this.streams[type] as Writable['write'])(message)
}

private addProcessExitListeners() {
const onExit = (signal?: string | number, exitCode?: number) => {
// Write buffered content on unexpected exits, e.g. direct `process.exit()` calls
this.flushBuffer()
this.stop()

// Interrupted signals don't set exit code automatically.
// Use same exit code as node: https://nodejs.org/api/process.html#signal-events
if (process.exitCode === undefined) {
process.exitCode = exitCode !== undefined ? (128 + exitCode) : Number(signal)
}

process.exit()
}

process.once('SIGINT', onExit)
process.once('SIGTERM', onExit)
process.once('exit', onExit)

return function cleanup() {
process.off('SIGINT', onExit)
process.off('SIGTERM', onExit)
process.off('exit', onExit)
}
}
}

/** Calculate the actual row count needed to render `rows` into `stream` */
Expand Down
26 changes: 0 additions & 26 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 80cde2a

Please sign in to comment.