moxios icon indicating copy to clipboard operation
moxios copied to clipboard

how to use moxios to return streams that have images

Open knihit opened this issue 4 years ago • 1 comments

I am trying to use moxios to mock the following scenario.

axios.default.get(imageUrl, { responseType: 'stream' })

To unit test the above, I am trying to write jest with the following.

    moxios.stubRequest('http://abc.com/text.jpg', {
        status: 200,
        response: { data: fs.createReadStream(`${__dirname}/text.png`) }
    });

But the response.data. When I use response.data.pipe(somewriteablestream) it does not work.

Any help appreciated for mocking a get request returning a stream object.

knihit avatar Jul 26 '20 02:07 knihit

Just pass fs.createReadStream directly.

moxios.stubRequest('/text.png', {
  status: 200,
  response: fs.createReadStream(`${__dirname}/text.png`),
})

nzwsch avatar Oct 17 '21 09:10 nzwsch