From d8bb74e44963c1d7d39c6e379593e7a71977f5ae Mon Sep 17 00:00:00 2001 From: Jeff Yates Date: Thu, 29 Aug 2024 15:47:46 -0500 Subject: [PATCH] [encoderpatch] Patch TextEncoder/TextDecoder for tests (#2307) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary: React 18 requires these things but JSDOM doesn't include them, so we shim them Issue: XXX-XXXX ## Test plan: `yarn test` before React 18 update and after. Author: somewhatabstract Reviewers: jeresig Required Reviewers: Approved By: jeresig Checks: ✅ Chromatic - Get results on regular PRs (ubuntu-latest, 20.x), ✅ Test (ubuntu-latest, 20.x, 2/2), ✅ Test (ubuntu-latest, 20.x, 1/2), ✅ Lint (ubuntu-latest, 20.x), ✅ Check build sizes (ubuntu-latest, 20.x), ✅ Chromatic - Build on regular PRs / chromatic (ubuntu-latest, 20.x), ⏭️ Chromatic - Skip on Release PR (changesets), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Prime node_modules cache for primary configuration (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ gerald, ⏭️ dependabot Pull Request URL: https://github.com/Khan/wonder-blocks/pull/2307 --- config/jest/test-setup.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/config/jest/test-setup.js b/config/jest/test-setup.js index f643bb02f..a41691966 100644 --- a/config/jest/test-setup.js +++ b/config/jest/test-setup.js @@ -3,9 +3,26 @@ const {StyleSheetTestUtils} = require("aphrodite"); const { mockRequestAnimationFrame, } = require("../../utils/testing/mock-request-animation-frame"); +const {TextEncoder, TextDecoder} = require("util"); StyleSheetTestUtils.suppressStyleInjection(); + +const attachShims = (targetWindow) => { + if (!targetWindow.TextEncoder) { + targetWindow.TextEncoder = TextEncoder; + } + if (!targetWindow.TextDecoder) { + targetWindow.TextDecoder = TextDecoder; + } +} + +const resetWindow = () => { + attachShims(globalThis); +}; +resetWindow(); + beforeEach(() => { + resetWindow(); mockRequestAnimationFrame(); });