Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gracefully exit when first SIGINT is received #3407

Merged
merged 1 commit into from May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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