0

I am writing Vitest reporter, when running tests in watch mode there is an active listener on keypress event on stdin. I want to have my own handling of user interaction, and this one pretty aggressively goes even into shortcuts that are part of few of its registered keys. My attempts at removing it were unsuccessful so far, i tried to use removeAllListeners on stdin, which didn't work and was unable to use removeListener which requires second argument which is a function I don't have access to.

Vitest code that registers keypress handler: Full related code including handler

function on() {
  off()
  rl = readline.createInterface({ input: stdin, escapeCodeTimeout: 50 })
  readline.emitKeypressEvents(stdin, rl)
  if (stdin.isTTY)
    stdin.setRawMode(true)
  stdin.on('keypress', keypressHandler)
}

My attempt

// neither remove attempt works

process.stdin.removeAllListeners()
process.stdin.removeAllListeners('keypress')

let rl: readline.Interface | undefined

readline.emitKeypressEvents(process.stdin, rl)

process.stdin.on('keypress', (_, key_data) => {  
  console.log(key_data)
})

My code that adds custom listener, it gets triggered only when Vitest's own handler is not triggered, so I need to preferably remove Vitest's listener, or at the very least change priority of mine so it gets triggered first, or somehow hide the Vitest listener.

0

Browse other questions tagged or ask your own question.