Skip to content

Commit

Permalink
fix: CTRL+C to terminate run (#3642)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jun 27, 2023
1 parent 189959e commit fa6637d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 1 addition & 3 deletions packages/vitest/src/node/cli-api.ts
Expand Up @@ -87,10 +87,8 @@ export async function startVitest(
return ctx
}

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

ctx.onServerRestart((reason) => {
ctx.report('onServerRestart', reason)
Expand Down
8 changes: 8 additions & 0 deletions packages/vitest/src/node/stdin.ts
Expand Up @@ -32,6 +32,10 @@ export function registerConsoleShortcuts(ctx: Vitest) {
// 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) {
ctx.logger.logUpdate.clear()
ctx.logger.log(c.red('Cancelling test run. Press CTRL+c again to exit forcefully.\n'))
process.exitCode = 130

await ctx.cancelCurrentRun('keyboard-input')
await ctx.runningPromise
}
Expand All @@ -45,6 +49,10 @@ export function registerConsoleShortcuts(ctx: Vitest) {
return
}

// Other keys are for watch mode only
if (!ctx.config.watch)
return

const name = key?.name

if (ctx.runningPromise) {
Expand Down

0 comments on commit fa6637d

Please sign in to comment.