-
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: 7 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.
Solution
Add
ReadeableStream
next toTextEncoder
and ourjest.pollyfills.js
will look so: