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.
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.
Here is the snippet that retrieves a file and pipe the response:
return new Promise((resolve, reject) => {
https.get(url, async response => {
response
.pipe(this.csvParser)
.pipe(crazyfunc)
.on("finish", () => {
logger.info("File process finished")
resolve()
})
.on("error", (err: Error) => {
if (err) {
reject(err)
}
})
resolve()
})
})
And here my .spec file, I mocking this call as follows:
const response = {
pipe: () => { },
on: () => { }
};
beforeEach(async () => {
spyOn(response, 'pipe').and.returnValue(response)
spyOn(response, 'on').and.returnValue(response)
});
spyOn(https, 'get').and.callFake((url, func) => func(response))