From 6e9daeba6fda8780ca3ffdf3e4c569f6898bcd65 Mon Sep 17 00:00:00 2001 From: Charley Wu Date: Thu, 9 Nov 2023 20:11:02 +0800 Subject: [PATCH] Fix ReferenceError: TextEncoder is not defined - https://stackoverflow.com/a/77372707 --- jest.config.js | 9 ++------- jest.environment.js | 11 +++++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 jest.environment.js diff --git a/jest.config.js b/jest.config.js index d4022c4e..db28ea21 100644 --- a/jest.config.js +++ b/jest.config.js @@ -10,13 +10,8 @@ module.exports = { "!src/environment/*.ts", "!src/mocks/*.js", ], + testEnvironment: "./jest.environment.js", testEnvironmentOptions: { customExportConditions: [''], - }, - globals: { - Request, - Response, - TextEncoder, - TextDecoder - }, + } }; diff --git a/jest.environment.js b/jest.environment.js new file mode 100644 index 00000000..840692a3 --- /dev/null +++ b/jest.environment.js @@ -0,0 +1,11 @@ +import Environment from "jest-environment-jsdom"; + +export default class CustomTestEnvironment extends Environment { + async setup() { + await super.setup(); + this.global.TextEncoder = TextEncoder; + this.global.TextDecoder = TextDecoder; + this.global.Response = Response; + this.global.Request = Request; + } +};