diff --git a/src/test/setup.ts b/src/test/setup.ts index e9083a3..b366e2a 100644 --- a/src/test/setup.ts +++ b/src/test/setup.ts @@ -1,3 +1,43 @@ +// jsdom does not reliably expose `localStorage` across environments (e.g. on +// Node 26 its experimental built-in `localStorage` shadows the jsdom one, so +// `localStorage` is undefined in tests). Provide a minimal in-memory shim so +// tests that rely on `localStorage` run consistently regardless of the runtime. +if (typeof globalThis.localStorage === 'undefined') { + class MemoryStorage { + private store = new Map(); + + get length(): number { + return this.store.size; + } + + key(index: number): string | null { + return Array.from(this.store.keys())[index] ?? null; + } + + getItem(key: string): string | null { + return this.store.get(key) ?? null; + } + + setItem(key: string, value: string): void { + this.store.set(key, String(value)); + } + + removeItem(key: string): void { + this.store.delete(key); + } + + clear(): void { + this.store.clear(); + } + } + + const storage = new MemoryStorage(); + globalThis.localStorage = storage as unknown as Storage; + if (typeof window !== 'undefined') { + window.localStorage = storage as unknown as Storage; + } +} + beforeEach(() => { if (typeof localStorage !== 'undefined') { localStorage.clear();