mardi 11 juillet 2017

How to pipe a stream into stdin while testing in javascript?

I need to pipe in a stream in the stdin of the terminal. Here's the code that needs to be tested.

var inquirer = require('inquirer');

function init() {
  var question = [{
       name: 'input',
       message: 'please provide some input text:',
       default: 'def'
  }];

  var input = '';

  function sampleCallback (answer)
  {
       console.log(answer.input);
       return;
  }

  inquirer.prompt(question).then(sampleCallback);

};

exports.init = init;

The test that I need to run is that wether the following code prints the input given or not. I wrote the following test but I can't figure out how to input the text for the above code.

var expect = require('chai').expect;
var initObject = require('../init');
var Readable = require('stream').Readable
var rs = new Readable;
rs.push('abc');
rs.push(null);

describe('Init Test', function(){
    it('should pass this canary test', function() {
      expect(true).to.be.true;
    });

    it('should display the text entered', function(){
        expect(rs.pipe(initObject.init())).to.be.eql('abc');
    });
});

please tell me where am I wrong.

Aucun commentaire:

Enregistrer un commentaire