GithubHelp home page GithubHelp logo

Comments (8)

kettanaito avatar kettanaito commented on May 24, 2024 2

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:

  1. undici tries to clone the response body using the standard structuredClone function. This is a global function in Node.js that can clone ReadableStream just fine.
// node_modules/undici/lib/fetch/body.js:277:21
const out2Clone = structuredClone(out2, { transfer: [out2] })
  1. But, since we are using Jest, we need to rely on core-js to polyfill structuredClone. The structuredClone from core-js just throws if the data it tries to transfer is ReadableStream, 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.

kettanaito avatar kettanaito commented on May 24, 2024 1

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.

kettanaito avatar kettanaito commented on May 24, 2024 1

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.

chawax avatar chawax commented on May 24, 2024

It looks like the problem I have too : #1915

from msw.

chawax avatar chawax commented on May 24, 2024

Yes, migrating to Vitest is definitely the only long term solution

from msw.

kettanaito avatar kettanaito commented on May 24, 2024

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.

chawax avatar chawax commented on May 24, 2024

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.

kettanaito avatar kettanaito commented on May 24, 2024

Related.

from msw.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.