msw: Consistent "TypeError: Failed to execute 'text' on 'Response': body stream already read" since v1.2.2

Prerequisites

Environment check

  • I’m using the latest msw version
  • I’m using Node.js version 14 or higher

Browsers

Chromium (Chrome, Brave, etc.)

Reproduction repository

https://github.com/JKapitein/msw-bodystream

Reproduction steps

yarn cypress-serve Can reach it locally from this point if you want, or yarn cypress-run

Current behavior

Failed to execute ‘text’ on ‘Response’: body stream already read

// src/utils/logging/serializeResponse.ts
var import_headers_polyfill9 = require("headers-polyfill");
async function serializeResponse(response2) {
  return {
    status: response2.status,
    statusText: response2.statusText,
    headers: (0, import_headers_polyfill9.flattenHeadersObject)((0, import_headers_polyfill9.headersToObject)(response2.headers)),
    body: await response2.text()
  };
}

Expected behavior

The request completes without throwing an error as in 1.2.1

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 46
  • Comments: 18 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Released: v1.2.3 🎉

This has been released in v1.2.3!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

I got the issue on any environment. downgrading to 1.2.1 (no carrot) fixed the issue.

I also consistently have this bug using either version 1.2.2 (node 16.19.1) or next (node 18.16.0). Works correctly with version 1.2.1.

I faced this issue also in version 1.2.1

Are you sure you did not just install the 1.2.2? If you use the ^ character in “msw”: “^1.2.1” it will download 1.2.2. The bug disappeared for me when using 1.2.1.

Having the same issue when using msw version 1.2.2. with @storybook/test-runner. No errors when using msw 1.2.1 🤷

@kettanaito I guess I was wrong with my comment, that I had it also with next. I rechecked my code and I saw that I have not triednext yet, so I guess, I had the problem with both node 16 and 18, but just with mws version 1.2.2.

@kettanaito I will try it with the next release and check if i still have this problem.

I am also getting the same error with v1.2.1. In my case, the error occurs when I make different HTTP requests, same path parameters and use multiple useQuery hooks at the same time. The error went away when I combined the two mock APIs used in the component error occured into one, but I’m having trouble because I originally wanted to separate these two APIs.

For what it’s worth, this happens very consistently to me when using Storybook, but if I use the built version yarn build-storybook and serve that, it does not happen.

I faced this issue also in version 1.2.1

Are you sure you did not just install the 1.2.2? If you use the ^ character in “msw”: “^1.2.1” it will download 1.2.2. The bug disappeared for me when using 1.2.1.

Thanks for replying. First of all sorry if I deleted the message but I didn’t see you answer.

Second you’re right, I am in a monorepo and didn’t notice one of the packages was using the 1.2.2 and all the versions were then resolved to the last one.

I confirm the bug relates only to the 1.2.2