Skip to content

How can I send ReadableStream as a mocked response body? #1769

Answered by kettanaito
kyawswarthwin asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, @kyawswarthwin. We add the support for ReadableStream mocked responses in MSW 2.0 (msw@next right now). This is how you can use it:

import { http, HttpResponse } from 'msw'

http.get('/resource', () => {
  const stream = new ReadableStream({
    start(controller) {
      // write chunks, don't forget to encode.
    }
  })

  // Send back the stream response.
  return new HttpResponse(stream)
})

ReadableStream is a global API in the browser and Node.js (v18+).

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by kettanaito
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants
Converted from issue

This discussion was converted from issue #1768 on October 15, 2023 10:09.