title: How to mock streams in NodeJS
body: <p>I'm attempting to unit test one of my node-js modules which deals heavily in streams. I'm trying to mock a stream (that I will write to), as within my module I have ".on('data/end)" listeners that I would like to trigger. Essentially I want to be able to do something like this:</p>
  
  <pre><code>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();
  </code></pre>
  
  <p>This executes, but the 'on' event never gets fired after I do the push (and .close() is invalid).</p>
  
  <p>All the guidance I can find on streams uses the 'fs' or 'net' library as a basis for creating a new stream (<a href="https://github.com/substack/stream-handbook" rel="noreferrer">https://github.com/substack/stream-handbook</a>), or they mock it out with sinon but the mocking gets very lengthy very quicky.</p>
  
  <p>Is there a nice way to provide a dummy stream like this?</p>