From 5e9376c6980b9867a6875937ac32534bd17a98f8 Mon Sep 17 00:00:00 2001 From: Matthew Dean Date: Thu, 11 Jan 2024 09:46:09 +0000 Subject: [PATCH] polyfil for TextDecoder in jsdom as according to https://github.com/jsdom/jsdom/issues/2524\#issuecomment-1118254830 --- jest-setup.ts | 12 ++++++++++++ jest.config.json | 4 ++++ package.json | 4 ++-- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 jest-setup.ts diff --git a/jest-setup.ts b/jest-setup.ts new file mode 100644 index 0000000..638cd6b --- /dev/null +++ b/jest-setup.ts @@ -0,0 +1,12 @@ +import * as util from 'util' + +// ref: https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom +// ref: https://github.com/jsdom/jsdom/issues/2524 +Object.defineProperty(window, 'TextEncoder', { + writable: true, + value: util.TextEncoder +}) +Object.defineProperty(window, 'TextDecoder', { + writable: true, + value: util.TextDecoder +}) diff --git a/jest.config.json b/jest.config.json index e7b7eb1..178ebba 100644 --- a/jest.config.json +++ b/jest.config.json @@ -20,6 +20,10 @@ "testPathIgnorePatterns": [ "/lib/" ], + "setupFiles": [ + "dotenv/config", + "./jest-setup.ts" + ], "testEnvironment": "jsdom", "moduleNameMapper": { "^(\\.{1,2}/.*\\.util)\\.js$": "$1.mock", diff --git a/package.json b/package.json index 89c8bf5..e521047 100755 --- a/package.json +++ b/package.json @@ -21,11 +21,11 @@ "clean": "rm -rf ./lib", "build": "npm run clean && tsc", "build:watch": "npm run clean && tsc --watch", - "coverage": "jest --coverage --setupFiles dotenv/config", + "coverage": "jest --coverage", "depcheck": "depcheck", "lint": "eslint .", "storybook": "storybook dev -p 9000", - "test": "jest --setupFiles dotenv/config" + "test": "jest" }, "peerDependencies": { "react": "^18.2.0"