Skip to content

fix(tests): Ensure ThemeFixture returns valid theme object for ThemeProvider#115781

Draft
sentry[bot] wants to merge 1 commit into
masterfrom
seer/fix/tests-theme-fixture-object
Draft

fix(tests): Ensure ThemeFixture returns valid theme object for ThemeProvider#115781
sentry[bot] wants to merge 1 commit into
masterfrom
seer/fix/tests-theme-fixture-object

Conversation

@sentry
Copy link
Copy Markdown
Contributor

@sentry sentry Bot commented May 19, 2026

This PR addresses a TypeError: Invalid value used as weak map key occurring in test environments, specifically within customRender in reactTestingLibrary.tsx.

Problem:
The error originated from @emotion/weak-memoize (used by @emotion/react's ThemeProvider) when the theme prop passed to ThemeProvider was not a valid object. This became a critical issue after the React 19 / Emotion 11.14 upgrade, as weak-memoize became stricter, throwing an error for non-object WeakMap keys (e.g., undefined or primitives).

In our test setup, customRender uses <ThemeProvider theme={ThemeFixture()}>. The ThemeFixture() function was returning lightTheme which, under certain module resolution timings or circular import scenarios, could resolve to undefined or a non-object value at the point it was used by ThemeProvider.

Solution:

  1. tests/js/fixtures/theme.tsx: Modified ThemeFixture to include a runtime check. It now explicitly verifies that lightTheme is a non-null object. If not, it throws a more descriptive error, preventing the opaque WeakMap error. It also returns a spread copy ({...lightTheme}) to ensure a fresh, plain object is always provided, preventing accidental mutations and ensuring compatibility with weak-memoize.
  2. tests/js/sentry-test/reactTestingLibrary.tsx: In customRender, the result of ThemeFixture() is now explicitly stored in a theme variable and validated to be a non-null object before being passed to <ThemeProvider>. This adds an additional layer of safety and provides an earlier, clearer error message if ThemeFixture somehow returns an invalid value.

These changes ensure that ThemeProvider always receives a valid object as its theme prop in tests, resolving the TypeError and making the test harness more robust against theme resolution issues.

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.

Fixes JEST-2KVF

@github-actions github-actions Bot added the Scope: Frontend Automatically applied to PRs that change frontend components label May 19, 2026
@github-actions
Copy link
Copy Markdown
Contributor

📊 Type Coverage Diff

✅ No new type safety issues introduced. Coverage: 93.56%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Frontend Automatically applied to PRs that change frontend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants