fix: TextEncoder not defined in Node.js test environments - #416
Open
thiagobarbosa wants to merge 1 commit into
Open
fix: TextEncoder not defined in Node.js test environments#416thiagobarbosa wants to merge 1 commit into
thiagobarbosa wants to merge 1 commit into
Conversation
Resolves spotify#268 spotify#268 Generated by issue-coder. Review before pushing.
thiagobarbosa
requested review from
AHB,
Billlynch,
andreas-karlsson,
fabriziodemaria,
mfranberg,
nicklasl,
sam-cook and
vahidlazio
as code owners
July 23, 2026 07:26
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #268
Problem
Confidence.showLoggerLink()picks its base64 encoder by sniffing forwindow:utf8ToBase64()(packages/sdk/src/utils.ts) callsnew TextEncoder(). The check assumes that "windowexists" implies "a full browser environment exists", which does not hold in simulated DOM test environments (e.g. Jest withtestEnvironment: 'jsdom', older jsdom versions, or any setup that defineswindowwithout polyfillingTextEncoder).In those environments the debug logger link throws
ReferenceError: TextEncoder is not defined, which surfaces during flag evaluation and breaks consumers' test suites even though nothing about the flag evaluation itself is wrong.Change
Widen the guard so it feature-detects
TextEncoderinstead of inferring it fromwindow, falling back toBufferwhen the encoder is unavailable:Real browsers are unaffected — they have both
windowandTextEncoder, so they keep taking theutf8ToBase64()path. Only the broken environment changes behaviour, and there it degrades to the Node encoder rather than throwing.Tests
Added a regression test in
packages/sdk/src/Confidence.test.tsthat:global.windowwhile deletingglobal.TextEncoder, simulating a DOM-ish environment without the encoder,window/TextEncoderglobals in afinallyblock so surrounding tests are unaffected.npx jest packages/sdk/src/Confidence.test.ts— 29 passed, including the new case.Files changed
packages/sdk/src/Confidence.tsTextEncoderbefore usingutf8ToBase64()packages/sdk/src/Confidence.test.tsTextEncoderenvironment