mswjs / msw

Industry standard API mocking for JavaScript.
https://mswjs.io
MIT License
15.97k stars 519 forks source link

Upgrade to 2.4.4 breaks ability to take request.json() #2296

Closed ailandini-accenture closed 1 month ago

ailandini-accenture commented 1 month ago

Prerequisites

Environment check

Node.js version

v20.17.0

Reproduction repository

cloning a repo should not be required for this. But if its a necessity I'll do it.

Reproduction steps

Adapt a request handler anywhere in your app akin to the following:

http.get('/authors'), async ({ request }) => {
    const body = await request.json();
    console.log(body);
    return HttpResponse.json({ results: [] });
});

This follows the documentation here

We are using vitest and our request library is react-query on top of fetch. We identified the issue was introduced in 2.4.4, but it persists in 2.4.9

Current behavior

Doing so at this level causes a Maximum call stack size exceeded error and immediate failures to any vitest test that fires off the intercepted request.

On a side note, we have a builder function which collects the request and serves it to the test later. IE:

function RequestBuilder() {
  const allCalls: Request[] = []
  http.get('/authors'), async () => {
    return HttpResponse.json({ results: [] });
  });
  return { getFirstCall: () => calls[0] };
}

it('some test', () => {
  const fetchAuthors = RequestBuilder();
  const req = fetchAuthors.getFirstCall()
  expect(await req.json()).toContainEqual(<some-expected-payload>).
});

Doing this returns an error because its trying to serialize the literal string "[object ReadableStream]" instead of the ReadableStream object

No issue in 2.4.3, and the issue is not resolved in latest (2.4.9).

I assume this must have something to do with changes in mswjs/interceptors. But as stated, in latest 0.35.8 this is not resolved.

Expected behavior

Executing await request.json() should return the request payload, as it did in 2.4.3.

kettanaito commented 1 month ago

Hi, @ailandini-accenture. Thanks for reporting this!

Please, could you provide a minimal reproduction repo for this? I have a bunch of similar scenarios passing in tests, so yours has to do something differently. Once you do, I'd be glad to look into this! Thanks.

ailandini-accenture commented 1 month ago

@kettanaito working through it in the sandbox we realized the issue was on our side, apologies for the inconvenience!

kettanaito commented 1 month ago

No worries! Glad you've figured it out.