title: Unit testing streams in node.js, jasmine
body: <p>I have a promise that calls an HTTP get, to retrieve ana csv file, this HTTP returns me a response that I can use as a stream, I piping the response through a csv parser, and thought another func that is a writable stream, and then I listen toa some events.</p>
  
  <p>But my problem is testing this, my problem is that I am not able to simulate the call to the events, I don't know how to reach the codes inside the finish and error.</p>
  
  <p>Here is the snippet that retrieves a file and pipe the response:</p>
  
  <pre><code>return new Promise((resolve, reject) =&gt; {
      https.get(url, async response =&gt; {
        response
          .pipe(this.csvParser)
          .pipe(crazyfunc)
          .on("finish", () =&gt; {
            logger.info("File process finished")
            resolve()
          })
          .on("error", (err: Error) =&gt; {
            if (err) {
              reject(err)
            }
          })
        resolve()
      })
    })
  </code></pre>
  
  <p>And here my .spec file, I mocking this call as follows:</p>
  
  <pre><code>const response = {
      pipe: () =&gt; { },
      on: () =&gt; { }
  };
  
  beforeEach(async () =&gt; {
      spyOn(response, 'pipe').and.returnValue(response)
      spyOn(response, 'on').and.returnValue(response)
  });
  
  spyOn(https, 'get').and.callFake((url, func) =&gt; func(response))
  </code></pre>