Skip to content

fix(tests): mock globalThis.localStorage before zustand persist init#28804

Open
travisbreaks wants to merge 1 commit intocalcom:mainfrom
travisbreaks:fix/onboarding-store-test-node22-localstorage
Open

fix(tests): mock globalThis.localStorage before zustand persist init#28804
travisbreaks wants to merge 1 commit intocalcom:mainfrom
travisbreaks:fix/onboarding-store-test-node22-localstorage

Conversation

@travisbreaks
Copy link
Copy Markdown

Summary

  • Fix 8 failing onboardingStore tests caused by Node.js 22+'s built-in globalThis.localStorage interfering with zustand's persist middleware
  • Move localStorage mock into vi.hoisted() so it runs before module imports, ensuring zustand captures a functional storage object
  • No changes to production code; test-only fix

Node.js 22+ ships a globalThis.localStorage that requires the --localstorage-file flag to function. Without it, the object exists but all methods (getItem, setItem, removeItem) are undefined. Zustand's persist middleware calls createJSONStorage(() => localStorage) during module initialization, capturing this broken reference before the test's Object.defineProperty mock could fix it.

Test plan

  • All 8 onboardingStore tests pass
  • Full vitest suite passes (630 files, 6819 tests, 0 failures)
  • Biome lint passes

Co-Authored-By: Tadao tadao@travisfixes.com

🤖 Generated with Claude Code

Node.js 22+ exposes a built-in globalThis.localStorage that requires
the --localstorage-file flag. Without it, the object exists but its
methods (getItem, setItem, removeItem) are undefined. Zustand's persist
middleware calls createJSONStorage(() => localStorage) at module init
time, capturing this broken reference before jsdom or test-level mocks
can replace it, causing "storage.setItem is not a function" in all 8
onboardingStore tests.

Move the localStorage mock into vi.hoisted() so it installs on
globalThis before module imports are evaluated. This ensures zustand's
persist middleware captures a functional storage object.

Co-Authored-By: Tadao <tadao@travisfixes.com>
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 9, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5855a89b-b821-4e2e-b8ed-df0b6a32dd01

📥 Commits

Reviewing files that changed from the base of the PR and between f3e07c5 and e31d2c1.

📒 Files selected for processing (1)
  • apps/web/modules/ee/organizations/lib/onboardingStore.test.ts

📝 Walkthrough

Walkthrough

A test file for the onboarding store module has been updated to improve localStorage mock initialization. The changes include adding a hoisted localStorage mock with functional methods (getItem, setItem, removeItem, clear, key, and length) to ensure proper Zustand persist storage handling. The test imports were adjusted, including removal of unused Next.js hook imports and reorganization of testing-related imports. The test setup now uses the hoisted localStorage mock instead of a direct declaration. These are test-only modifications with no changes to public entity signatures.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: mocking globalThis.localStorage before Zustand's persist initialization to fix failing tests.
Description check ✅ Passed The description is directly related to the changeset, explaining the Node.js 22+ localStorage issue and the vi.hoisted() solution for the failing tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants