fix(tests): mock globalThis.localStorage before zustand persist init#28804
fix(tests): mock globalThis.localStorage before zustand persist init#28804travisbreaks wants to merge 1 commit intocalcom:mainfrom
Conversation
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>
|
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughA 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)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Summary
globalThis.localStorageinterfering with zustand's persist middlewarevi.hoisted()so it runs before module imports, ensuring zustand captures a functional storage objectNode.js 22+ ships a
globalThis.localStoragethat requires the--localstorage-fileflag to function. Without it, the object exists but all methods (getItem,setItem,removeItem) areundefined. Zustand'spersistmiddleware callscreateJSONStorage(() => localStorage)during module initialization, capturing this broken reference before the test'sObject.definePropertymock could fix it.Test plan
Co-Authored-By: Tadao tadao@travisfixes.com
🤖 Generated with Claude Code