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