Skip to content

Commit 97875c3

Browse files
committed
refactor: replace signal-exit with built-in solution
1 parent d659260 commit 97875c3

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

packages/vitest/package.json

Lines changed: 0 additions & 1 deletion
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-
"signal-exit": "^3.0.7",
166165
"std-env": "^3.8.0",
167166
"tinybench": "^2.9.0",
168167
"tinyexec": "^0.3.1",

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

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import type { Writable } from 'node:stream'
22
import type { Vitest } from '../../core'
33
import { stripVTControlCharacters } from 'node:util'
44

5-
// @ts-expect-error -- untyped, cannot use v4 due to other deps
6-
import onExit from 'signal-exit'
7-
85
const DEFAULT_RENDER_INTERVAL = 16
96

107
const ESC = '\x1B['
@@ -48,15 +45,10 @@ export class WindowRenderer {
4845
error: options.logger.errorStream.write.bind(options.logger.errorStream),
4946
}
5047

51-
// Write buffered content on unexpected exits, e.g. direct `process.exit()` calls
52-
onExit(() => {
53-
this.flushBuffer()
54-
this.write(SHOW_CURSOR)
55-
})
56-
5748
this.cleanups.push(
5849
this.interceptStream(process.stdout, 'output'),
5950
this.interceptStream(process.stderr, 'error'),
51+
this.addProcessExitListeners(),
6052
)
6153

6254
this.write(HIDE_CURSOR, 'output')
@@ -182,6 +174,38 @@ export class WindowRenderer {
182174
private write(message: string, type: 'output' | 'error' = 'output') {
183175
(this.streams[type] as Writable['write'])(message)
184176
}
177+
178+
private addProcessExitListeners() {
179+
const onExit = (signal?: string | 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 (signal === 'SIGINT') {
187+
process.exitCode = 130
188+
}
189+
else if (signal === 'SIGTERM') {
190+
process.exitCode = 143
191+
}
192+
else {
193+
process.exitCode = Number(signal)
194+
}
195+
196+
process.exit()
197+
}
198+
199+
process.once('SIGINT', onExit)
200+
process.once('SIGTERM', onExit)
201+
process.once('exit', onExit)
202+
203+
return function cleanup() {
204+
process.off('SIGINT', onExit)
205+
process.off('SIGTERM', onExit)
206+
process.off('exit', onExit)
207+
}
208+
}
185209
}
186210

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

pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)