MCPcopy
hub / github.com/vitest-dev/vitest / registerConsoleShortcuts

Function registerConsoleShortcuts

packages/vitest/src/node/stdin.ts:75–273  ·  view source on GitHub ↗
(
  ctx: Vitest,
  stdin: NodeJS.ReadStream | undefined = process.stdin,
  stdout: NodeJS.WriteStream | Writable,
)

Source from the content-addressed store, hash-verified

73}
74
75export function registerConsoleShortcuts(
76 ctx: Vitest,
77 stdin: NodeJS.ReadStream | undefined = process.stdin,
78 stdout: NodeJS.WriteStream | Writable,
79) {
80 let latestFilename = ''
81
82 async function _keypressHandler(str: string, key: any) {
83 // Cancel run and exit when ctrl-c or esc is pressed.
84 // If cancelling takes long and key is pressed multiple times, exit forcefully.
85 if (
86 str === '\x03'
87 || str === '\x1B'
88 || (key && key.ctrl && key.name === 'c')
89 ) {
90 if (!ctx.isCancelling) {
91 ctx.logger.log(
92 c.red('Cancelling test run. Press CTRL+c again to exit forcefully.\n'),
93 )
94 process.exitCode = 130
95
96 await ctx.cancelCurrentRun('keyboard-input')
97 }
98 return ctx.exit(true)
99 }
100
101 // window not support suspend
102 if (!isWindows && key && key.ctrl && key.name === 'z') {
103 process.kill(process.ppid, 'SIGTSTP')
104 process.kill(process.pid, 'SIGTSTP')
105 return
106 }
107
108 const name = key?.name
109
110 if (ctx.runningPromise) {
111 if (cancelKeys.includes(name)) {
112 await ctx.cancelCurrentRun('keyboard-input')
113 }
114 return
115 }
116
117 // quit
118 if (name === 'q') {
119 return ctx.exit(true)
120 }
121
122 // help
123 if (name === 'h') {
124 return printShortcutsHelp()
125 }
126 // update snapshot
127 if (name === 'u') {
128 return ctx.updateSnapshot()
129 }
130 // rerun all tests
131 if (name === 'a' || name === 'return') {
132 const files = await ctx._globTestFilepaths()

Callers 2

startVitestFunction · 0.90
cancel-run.test.tsFile · 0.90

Calls 2

onFunction · 0.70
offFunction · 0.70

Tested by

no test coverage detected