fix: mock localStorage in test setup for Zustand persist middleware
This commit is contained in:
@@ -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 })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -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,
|
||||||
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user