diff --git a/packages/vitest/src/node/cli-api.ts b/packages/vitest/src/node/cli-api.ts index 84b280f79b1d..d68d0306a9ad 100644 --- a/packages/vitest/src/node/cli-api.ts +++ b/packages/vitest/src/node/cli-api.ts @@ -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) diff --git a/packages/vitest/src/node/stdin.ts b/packages/vitest/src/node/stdin.ts index 9bc3485125dd..460d42235ad6 100644 --- a/packages/vitest/src/node/stdin.ts +++ b/packages/vitest/src/node/stdin.ts @@ -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 } @@ -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) {