Skip to content

Commit d568f8c

Browse files
authored
fix(cli): exit on CTRL+c even when globalSetup runs blocked code (#10863)
1 parent 64a5731 commit d568f8c

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

‎packages/vitest/src/node/stdin.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,12 @@ export function registerConsoleShortcuts(
9191
)
9292
process.exitCode = 130
9393

94+
// Unregister raw mode so that second CTRL+c is handled by Node.js as SIGINT
95+
off()
96+
9497
await ctx.cancelCurrentRun('keyboard-input')
9598
}
99+
96100
return ctx.exit(true)
97101
}
98102

‎test/e2e/test/cancel-run.test.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test('can force cancel a run via CLI', async () => {
3131

3232
const stdin = new Readable({ read: () => '' }) as NodeJS.ReadStream
3333
stdin.isTTY = true
34-
stdin.setRawMode = () => stdin
34+
stdin.setRawMode = vi.fn().mockReturnValue(stdin)
3535
registerConsoleShortcuts(vitest, stdin, new Writable())
3636

3737
const onLog = vi.spyOn(vitest.logger, 'log').mockImplementation(() => {})
@@ -48,8 +48,11 @@ test('can force cancel a run via CLI', async () => {
4848
const logs = onLog.mock.calls.map(log => stripVTControlCharacters(log[0] || '').trim())
4949
expect(logs).toContain('Cancelling test run. Press CTRL+c again to exit forcefully.')
5050

51-
// Second CTRL+c should stop run
52-
stdin.emit('data', CTRL_C)
51+
// Second CTRL+c should stop run. Raw mode should be disabled so Node handles the second CTRL+c as SIGINT and exit forcefully.
52+
expect.soft(stdin.setRawMode).toHaveBeenLastCalledWith(false)
53+
54+
// Test cleanup:
55+
vitest.exit(true)
5356
await promise
5457

5558
// `exit()` calls `process.exit` only after `close()` finishes — poll instead

0 commit comments

Comments
 (0)