I'm trying to write a test for a CLI that uses Node.js's readline module to print and capture info from the user, and I can't seem to capture anything from stdout. A simple version of the issue I'm facing follows.
app.js:
#!/usr/bin/env node const readline = require('readline') const rl = readline.createInterface({ input: process.stdin, output: process.stdout }) rl.write('hello\n') process.exit() runner.js:
const spawn = require('child_process').spawn const rl = spawn(__dirname + '/app.js') rl.stdout.on('data', chunk => { console.log('stdout says', chunk.toString()) }) running runner.js, I would expect to see the output stdout says hello, but nothing prints.
However, if I run app.js directly, hello prints out to the console. Additionally, if I use other readline methods such as question, the handler will fire with the expected data.
Why doesn't this code work as expected? How can it be changed to work?