diff --git a/apps/web/vitest.setup.ts b/apps/web/vitest.setup.ts index 4523022..92165be 100644 --- a/apps/web/vitest.setup.ts +++ b/apps/web/vitest.setup.ts @@ -24,3 +24,33 @@ Object.defineProperty(window, "matchMedia", { dispatchEvent: () => false, }), }); + +// Ensure localStorage exists with a full Storage API for tests. +// Avoid touching the built-in accessor (which emits warnings in Node). +let storageStore: Record = {}; +const storageMock: Storage = { + getItem: (key: string): string | null => storageStore[key] ?? null, + setItem: (key: string, value: string): void => { + storageStore[key] = value; + }, + removeItem: (key: string): void => { + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete + delete storageStore[key]; + }, + clear: (): void => { + storageStore = {}; + }, + get length(): number { + return Object.keys(storageStore).length; + }, + key: (index: number): string | null => { + const keys = Object.keys(storageStore); + return keys[index] ?? null; + }, +}; + +Object.defineProperty(window, "localStorage", { + value: storageMock, + writable: true, + configurable: true, +});