shlogg · Early preview
Benjamin G. @ger-benjamin

Node.js CLI Application Testing With Readline Module

Testing CLI apps with Node.js: Use `vitest` & `spyOn` to mock console.log, overload console.log to redirect output, or add a "debug" mode to store logs. Simulate writing with `stdin.emit('keypress')`. Debug with node --inspect and Webkit browser.

Node.js version: 22.1.0.
Part one - the basics - is here: https://dev.to/camptocamp-geo/cli-application-with-the-nodejs-readline-module-48ic
Part two - going further - is here: https://dev.to/camptocamp-geo/cli-application-with-the-nodejs-readline-module-23-2ekg

  
  
  Testing

A program is never complete without tests. In addition to unit tests, which are generally context-free, I tried to do a more global test. More or less an acceptance test for my CLI program. I was faced with two problems:

How to read my console.
How to simulate writing.

  
  
  Read

To read what comes out of console...