mercredi 31 août 2016

How to mock readline.on('SIGINT')?

I have this piece of code:

function getMsg() {
  return new Promise(function (resolve, reject) {
    var input = [];
    var rl = readline.createInterface({
      input: process.stdin,
      output: process.stdout
    });
    rl.on('line', function (cmd) {
      if (cmd.trim()) {
        input.push(cmd);
      } else {
        rl.close();
      }
    });
    rl.on('close', function () {
      rl.close();
      resolve(input.join('\n'));
    });
    rl.on('SIGINT', reject);
  });
}

I'm trying to test this function, my attempt, so far, is this:

it('should reject if SIGINT is sent', function () {
  sandbox.stub(readline, 'createInterface', function () {
    return {
      on: function (action, callback) {
        callback();
      },
      prompt: function () {},
      close: function () {}
    };
  });

  return getMsg().then(null).catch(function () {
    expect(true).to.be.equal(true);
  });
});

But of course, that doesn't simulate a SIGINT, how do I do this?

Aucun commentaire:

Enregistrer un commentaire