Comments (8)
Hi. Thanks for reporting this. @chawax, thanks for the extensive discussion. Let's get to the bottom of this.
Root cause
First, let's see why the request fails. If we inspect the actual error preserved by Undici, we see this:
DataCloneError: Transferring of ReadableStream cannot be properly polyfilled in this engine
at throwUnpolyfillable (/msw-request-with-body-ignored/node_modules/core-js/modules/web.structured-clone.js:120:9)
at tryToTransfer (/msw-request-with-body-ignored/node_modules/core-js/modules/web.structured-clone.js:485:9)
at structuredClone (/msw-request-with-body-ignored/node_modules/core-js/modules/web.structured-clone.js:520:17)
at cloneBody (/msw-request-with-body-ignored/node_modules/undici/lib/fetch/body.js:277:21)
This hints that Jest is having serious trouble with ReadableStream
.
This error is caused by core-js
. One of our community members shared that removing core-js
solved this error for them: mswjs/interceptors#475 (comment).
To be more specific, this is what happens:
- undici tries to clone the response body using the standard
structuredClone
function. This is a global function in Node.js that can cloneReadableStream
just fine.
// node_modules/undici/lib/fetch/body.js:277:21
const out2Clone = structuredClone(out2, { transfer: [out2] })
- But, since we are using Jest, we need to rely on
core-js
to polyfillstructuredClone
. ThestructuredClone
fromcore-js
just throws if the data it tries to transfer isReadableStream
, claiming it as unpolyfillable:
// node_modules/core-js/modules/web.structured-clone.js:485:9
switch (type) {
...
case 'MediaSourceHandle':
case 'MessagePort':
case 'OffscreenCanvas':
case 'ReadableStream':
case 'TransformStream':
case 'WritableStream':
throwUnpolyfillable(type, TRANSFERRING);
}
This is a bold statement, especially since we polyfill ReadableStream
in jest.polyfills.js
. But core-js
never checks for that, just assumes a no-op and throws.
The solution seems to be, once again, force Jest to use the platform, to use Node.js where things function correctly.
from msw.
Jest depends on core-js
, there's nothing we can do about that. Node.js doesn't provide the means to directly import structuredClone
, it's set directly on the internal messaging
binding in C++. No way to suggest require('node:???').structuredClone
to "polyfill" it in jest.polyfills.js
.
There's literally nothing I can do here. Jest doesn't respect your environment. I highly recommend you migrate away from Jest to tools like Vitest and forget about these sorts of errors. I know it's not ideal, I know migrations take time, but it's either one-time migration or lifetime of dealing with this shenanigans that nobody but Jest can properly address.
from msw.
Some folks are having success by downgrading undici to v5: #1934 (reply in thread). This may be a temporary option for those who cannot migrate to other testing frameworks as of now.
from msw.
It looks like the problem I have too : #1915
from msw.
Yes, migrating to Vitest is definitely the only long term solution
from msw.
Also a note of warning: this seems to be specific to JSDOM. ReadableStream
and other Node.js globals work fine in Jest's node
environment. But since jest-environment-jsdom
isn't based on jest-environment-node
, you don't get that. You can try swapping it with HappyDOM to see if they respect the Node.js globals better.
from msw.
Well, I was using HappyDOM in my project, and it looks like that was the reason why downgrading Undici didn't work for me. Using Jest Environement JSDom solved instead of HappyDOM solved my problem indeed.
from msw.
from msw.
Related Issues (20)
- React Native: server.use does not add request handlers HOT 3
- JSON response with special chars truncated since 2.2.0 HOT 3
- Cannot load the application if the browser doesn't have slash at the end of the URL
- MSW does not intercept requests when the node http(s) library is imported using * as HOT 4
- Mocked stream response with multiple enqueues hangs process HOT 6
- Browser: Cannot read properties of undefined (reading 'url') HOT 4
- Wrong url for mockServiceWorker.js when base config set in Vite 5.x config HOT 2
- Add delay before each request HOT 3
- Mock a request that contains both query and path parameters
- TypeError: confirm is not a function HOT 7
- Failing to intercept an Axios request: Node 20.11 + Vitest 1.3.1 + MSW 2.2.1
- v2.2.2 does not intercept request in browser mode (CORS error) but v1.3.1 does HOT 3
- Infering the `boundary` callback arguments HOT 10
- support custom fetch option HOT 7
- support selecting interceptors HOT 1
- Set-Cookie responses containing commas are not handled correctly HOT 1
- HttpResponse.json() throwing TypeError: Right-hand side of 'instanceof' is not an object. HOT 1
- Request with FormData body makes Jest hang forever HOT 6
- TypeError: Right-hand side of 'instanceof' is not an object HOT 4
- Cannot read properties of undefined (reading 'url') HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msw.