fix: mock localStorage in test setup for Zustand persist middleware

This commit is contained in:
Pouzor
2026-03-07 00:47:51 +01:00
parent 0bd714a68b
commit 923f0c0c22
2 changed files with 14 additions and 0 deletions
@@ -3,6 +3,7 @@ import { useAuthStore } from '@/stores/authStore'
describe('authStore', () => { describe('authStore', () => {
beforeEach(() => { beforeEach(() => {
localStorage.clear()
useAuthStore.setState({ token: null, isAuthenticated: false }) useAuthStore.setState({ token: null, isAuthenticated: false })
}) })
+13
View File
@@ -1 +1,14 @@
import '@testing-library/jest-dom' import '@testing-library/jest-dom'
// Zustand persist middleware requires localStorage. jsdom provides it but
// some test runners strip it — ensure a working implementation is always present.
const store: Record<string, string> = {}
Object.defineProperty(globalThis, 'localStorage', {
value: {
getItem: (k: string) => store[k] ?? null,
setItem: (k: string, v: string) => { store[k] = v },
removeItem: (k: string) => { delete store[k] },
clear: () => { Object.keys(store).forEach((k) => delete store[k]) },
},
writable: true,
})