node.js - How to mock streams in NodeJS -


i'm attempting unit test 1 of node-js modules deals heavily in streams. i'm trying mock stream (that write to), within module have ".on('data/end)" listeners trigger. want able this:

var mockedstream = new require('stream').readable();  mockedstream.on('data', function withdata('data') {   console.dir(data); });  mockedstream.on('end', function() {    console.dir('goodbye'); });  mockedstream.push('hello world'); mockedstream.close(); 

this executes, 'on' event never gets fired after push (and .close() invalid).

all guidance can find on streams uses 'fs' or 'net' library basis creating new stream (https://github.com/substack/stream-handbook), or mock out sinon mocking gets lengthy quicky.

is there nice way provide dummy stream this?

instead of using push, should have been using ".emit(<event>, <data>);"

my mock code works , looks like:

var mockedstream = new require('stream').readable(); mockedstream._read = function(size) { /* nothing */ };  mymodule.functioniwanttotest(mockedstream); // has .on() listeners in  mockedstream.emit('data', 'hello data!'); mockedstream.emit('end'); 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -