Skip to content

Commit

Permalink
fix: gracefully exit when first SIGINT is received (#3407)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed May 24, 2023
1 parent 632eef4 commit a2cc2b3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/vitest/src/node/cli-api.ts
Expand Up @@ -79,6 +79,8 @@ export async function startVitest(

if (process.stdin.isTTY && ctx.config.watch)
registerConsoleShortcuts(ctx)
else
process.on('SIGINT', () => ctx.cancelCurrentRun('keyboard-input'))

ctx.onServerRestart((reason) => {
ctx.report('onServerRestart', reason)
Expand Down
10 changes: 8 additions & 2 deletions packages/vitest/src/node/stdin.ts
Expand Up @@ -28,9 +28,15 @@ export function registerConsoleShortcuts(ctx: Vitest) {
let latestFilename = ''

async function _keypressHandler(str: string, key: any) {
// ctrl-c or esc
if (str === '\x03' || str === '\x1B' || (key && key.ctrl && key.name === 'c'))
// Cancel run and exit when ctrl-c or esc is pressed.
// If cancelling takes long and key is pressed multiple times, exit forcefully.
if (str === '\x03' || str === '\x1B' || (key && key.ctrl && key.name === 'c')) {
if (!ctx.isCancelling) {
await ctx.cancelCurrentRun('keyboard-input')
await ctx.runningPromise
}
return ctx.exit(true)
}

// window not support suspend
if (!isWindows && key && key.ctrl && key.name === 'z') {
Expand Down

0 comments on commit a2cc2b3

Please sign in to comment.