-
after including the jest.polyfills.js and the code from the official docs and installing undici. I have the error: ReferenceError: ReadableStream is not defined when I try to execute a test. Help :( |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 24 replies
-
SolutionAdd // jest.polyfills.js
/**
* @note The block below contains polyfills for Node.js globals
* required for Jest to function when running JSDOM tests.
* These HAVE to be require's and HAVE to be in this exact
* order, since "undici" depends on the "TextEncoder" global API.
*
* Consider migrating to a more modern test runner if
* you don't want to deal with this.
*/
const { TextDecoder, TextEncoder, ReadableStream } = require("node:util")
Object.defineProperties(globalThis, {
TextDecoder: { value: TextDecoder },
TextEncoder: { value: TextEncoder },
ReadableStream: { value: ReadableStream },
})
const { Blob, File } = require("node:buffer")
const { fetch, Headers, FormData, Request, Response } = require("undici")
Object.defineProperties(globalThis, {
fetch: { value: fetch, writable: true },
Blob: { value: Blob },
File: { value: File },
Headers: { value: Headers },
FormData: { value: FormData },
Request: { value: Request },
Response: { value: Response },
}) |
Beta Was this translation helpful? Give feedback.
-
I have the same issue, tried with several node versions v18+ |
Beta Was this translation helpful? Give feedback.
-
Just upgrade your node js version to lates like 20.11.1 or something upgraded |
Beta Was this translation helpful? Give feedback.
-
An upgrade to node v20 did not help for me. const { TextDecoder, TextEncoder } = require('node:util') I hope it helps! |
Beta Was this translation helpful? Give feedback.
-
msw.js have created https://github.com/mswjs/jest-fixed-jsdom to provide a Jest configuration that already includes as much as for the needed "fixes" as possible |
Beta Was this translation helpful? Give feedback.
-
In my case with node version 18 Issue is resolved, try node v18 or 18+ |
Beta Was this translation helpful? Give feedback.
-
To understand, we have the same issue in one of our projects, but in another, we are running the same environment configurations and dependencies. It works without the use of the declaration of ReadeableStream. |
Beta Was this translation helpful? Give feedback.
-
I am also having this error with vitest running in node mode with jsdom. I cannot switch to browser mode, as this is experimental and requires significant work. Also, it doesn't work with my CI flow easily. I also cannot use jest-fixed-jsdom as vitest doesnt know how to load this? Instead, we should be able to run msw vitest with jsdom. Can you please release a vitest-environment-fixed-jsom? |
Beta Was this translation helpful? Give feedback.
-
I had the same issue node with 16.20.2, after updating it to 20.17.0 fixed the issue. This was because my project was not compatible with node 16. |
Beta Was this translation helpful? Give feedback.
Solution
Add
ReadeableStream
next toTextEncoder
and ourjest.pollyfills.js
will look so: