@@ -2,9 +2,6 @@ import type { Writable } from 'node:stream'
2
2
import type { Vitest } from '../../core'
3
3
import { stripVTControlCharacters } from 'node:util'
4
4
5
- // @ts -expect-error -- untyped, cannot use v4 due to other deps
6
- import onExit from 'signal-exit'
7
-
8
5
const DEFAULT_RENDER_INTERVAL = 16
9
6
10
7
const ESC = '\x1B['
@@ -48,15 +45,10 @@ export class WindowRenderer {
48
45
error : options . logger . errorStream . write . bind ( options . logger . errorStream ) ,
49
46
}
50
47
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
-
57
48
this . cleanups . push (
58
49
this . interceptStream ( process . stdout , 'output' ) ,
59
50
this . interceptStream ( process . stderr , 'error' ) ,
51
+ this . addProcessExitListeners ( ) ,
60
52
)
61
53
62
54
this . write ( HIDE_CURSOR , 'output' )
@@ -182,6 +174,38 @@ export class WindowRenderer {
182
174
private write ( message : string , type : 'output' | 'error' = 'output' ) {
183
175
( this . streams [ type ] as Writable [ 'write' ] ) ( message )
184
176
}
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
+ }
185
209
}
186
210
187
211
/** Calculate the actual row count needed to render `rows` into `stream` */
0 commit comments