diff --git a/test/jest-e2e.json b/test/jest-e2e.json index 40c7cb5e..0a2a5d40 100644 --- a/test/jest-e2e.json +++ b/test/jest-e2e.json @@ -5,6 +5,7 @@ "moduleNameMapper": { "^build-info$": "/build-info.json" }, + "setupFiles": ["/test/jest.polyfills.js"], "testEnvironment": "node", "testRegex": ".e2e-spec.ts$", "transform": { diff --git a/test/jest.polyfills.js b/test/jest.polyfills.js new file mode 100644 index 00000000..28e94531 --- /dev/null +++ b/test/jest.polyfills.js @@ -0,0 +1,31 @@ +// 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 }, +})