mswjs / interceptors

Low-level network interception library.
https://npm.im/@mswjs/interceptors
MIT License
535 stars 120 forks source link

ReadableStream cannot be properly polyfilled in this engine #475

Closed davidtaboas closed 10 months ago

davidtaboas commented 10 months ago

Using msw 2.0.2 I'm getting the following error:

 DataCloneError: Transferring of ReadableStream cannot be properly polyfilled in this engine
        at throwUnpolyfillable ([root_project]/node_modules/core-js/modules/web.structured-clone.js:127:9)
        at tryToTransfer ([root_project]/node_modules/core-js/modules/web.structured-clone.js:579:9)
        at structuredClone ([root_project]/node_modules/core-js/modules/web.structured-clone.js:622:17)
        at cloneBody ([root_project]/node_modules/undici/lib/fetch/body.js:282:21)
        at cloneResponse ([root_project]/node_modules/undici/lib/fetch/response.js:319:24)
        at HttpResponse.clone ([root_project]/node_modules/undici/lib/fetch/response.js:259:28)
        at globalThis.fetch ([root_project]/node_modules/@mswjs/interceptors/lib/node/interceptors/fetch/index.js:92:46)
        at processTicksAndRejections (node:internal/process/task_queues:95:5)

If i change in this line https://github.com/mswjs/interceptors/blob/3258f5cc62c472ba63f5c70e455d5b245c74a216/src/interceptors/fetch/index.ts#L122

to this, works properly:

const responseClone = new Response(mockedResponse);
kettanaito commented 10 months ago

Hey, @davidtaboas. Thanks for reporting this.

Please, could you share a reproduction repository for this? It looks like you are running your code in an environment that doesn't correctly implement the global Fetch API (not sure why core-js is even needed here). I cannot help much without the way to reproduce this.

davidtaboas commented 10 months ago

@kettanaito ok. i will try it.

I'm using msw in a jsdom environment with jest, using undici to polyfill fetch.

kettanaito commented 10 months ago

Take a look at the Jest+MSW usage example. It may help you spot what may be off.

Why do you need to polyfill fetch? What version of Node.js are you using? Make sure to use Node.js v18+ with MSW 2.0.

davidtaboas commented 10 months ago

@kettanaito I did, thanks.

But it was no help. However, I removed "core-js" import from my jest.polyfills.js and now it seems to work properly. I will double check a few more times. Meanwhile this issue can still closed.

Thanks!

kettanaito commented 7 months ago

The issue is that Jest depends on core-js anyway. Although they are grabbing global.structuredClone, it appears it's already polyfilled by core-js at that moment of access.

https://github.com/jestjs/jest/pull/12631/files#diff-7e6b39bebf54c704e027085672211430b8241b080e103d997757bcea915d2041R102