Skip to content

Commit 80cde2a

Browse files
authoredNov 26, 2024··
fix(reporters): write buffered stdout/stderr on process exit (#6932)
1 parent a5bbc0a commit 80cde2a

File tree

5 files changed

+28
-54
lines changed

5 files changed

+28
-54
lines changed
 

‎packages/vitest/LICENSE.md

-24
Original file line numberDiff line numberDiff line change
@@ -1245,30 +1245,6 @@ Repository: git://github.com/feross/run-parallel.git
12451245
12461246
---------------------------------------
12471247

1248-
## signal-exit
1249-
License: ISC
1250-
By: Ben Coe
1251-
Repository: https://github.com/tapjs/signal-exit.git
1252-
1253-
> The ISC License
1254-
>
1255-
> Copyright (c) 2015, Contributors
1256-
>
1257-
> Permission to use, copy, modify, and/or distribute this software
1258-
> for any purpose with or without fee is hereby granted, provided
1259-
> that the above copyright notice and this permission notice
1260-
> appear in all copies.
1261-
>
1262-
> THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1263-
> WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
1264-
> OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE
1265-
> LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
1266-
> OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
1267-
> WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
1268-
> ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1269-
1270-
---------------------------------------
1271-
12721248
## sisteransi
12731249
License: MIT
12741250
By: Terkel Gjervig

‎packages/vitest/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@
162162
"expect-type": "^1.1.0",
163163
"magic-string": "^0.30.12",
164164
"pathe": "^1.1.2",
165-
"restore-cursor": "^5.1.0",
166165
"std-env": "^3.8.0",
167166
"tinybench": "^2.9.0",
168167
"tinyexec": "^0.3.1",

‎packages/vitest/src/node/reporters/base.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export abstract class BaseReporter implements Reporter {
140140
}
141141

142142
else if (this.renderSucceed || anyFailed) {
143-
this.log(` ${c.green(c.dim(F_CHECK))} ${getTestName(test, c.dim(' > '))}`)
143+
this.log(` ${c.dim(getStateSymbol(test))} ${getTestName(test, c.dim(' > '))}`)
144144
}
145145
}
146146
}

‎packages/vitest/src/node/reporters/renderers/windowedRenderer.ts

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Writable } from 'node:stream'
22
import type { Vitest } from '../../core'
33
import { stripVTControlCharacters } from 'node:util'
4-
import restoreCursor from 'restore-cursor'
54

65
const DEFAULT_RENDER_INTERVAL = 16
76

@@ -49,9 +48,9 @@ export class WindowRenderer {
4948
this.cleanups.push(
5049
this.interceptStream(process.stdout, 'output'),
5150
this.interceptStream(process.stderr, 'error'),
51+
this.addProcessExitListeners(),
5252
)
5353

54-
restoreCursor()
5554
this.write(HIDE_CURSOR, 'output')
5655

5756
this.start()
@@ -175,6 +174,32 @@ export class WindowRenderer {
175174
private write(message: string, type: 'output' | 'error' = 'output') {
176175
(this.streams[type] as Writable['write'])(message)
177176
}
177+
178+
private addProcessExitListeners() {
179+
const onExit = (signal?: string | number, exitCode?: number) => {
180+
// Write buffered content on unexpected exits, e.g. direct `process.exit()` calls
181+
this.flushBuffer()
182+
this.stop()
183+
184+
// Interrupted signals don't set exit code automatically.
185+
// Use same exit code as node: https://nodejs.org/api/process.html#signal-events
186+
if (process.exitCode === undefined) {
187+
process.exitCode = exitCode !== undefined ? (128 + exitCode) : Number(signal)
188+
}
189+
190+
process.exit()
191+
}
192+
193+
process.once('SIGINT', onExit)
194+
process.once('SIGTERM', onExit)
195+
process.once('exit', onExit)
196+
197+
return function cleanup() {
198+
process.off('SIGINT', onExit)
199+
process.off('SIGTERM', onExit)
200+
process.off('exit', onExit)
201+
}
202+
}
178203
}
179204

180205
/** Calculate the actual row count needed to render `rows` into `stream` */

‎pnpm-lock.yaml

-26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.