samedi 22 septembre 2018

NodeJS write to stdin in jest tests

I have a command-line app that reads user input from stdin:

const readline = require('readline');
const os = require('os');

const rl = readline.createInterface({
  input: process.stdin,
});

rl.on('line', (line) => {
  try {
    doSomething();
  } catch (e) {
    process.stdout.write(`Error: ${e.message}${os.EOL}`);
  }
});

To write integration tests, I need to be able to write to stdin to activate the 'line' event. However, calling process.stdin.write(`abc${os.EOL}`) in my test case produces an error:

This socket is closed

How should I resolve this?

Is the only option to skip the command-line integration testing and test doSomething only?

Aucun commentaire:

Enregistrer un commentaire