From 3cbd69bc7f30885a5ca5afcbf9fae2d069fdd74c Mon Sep 17 00:00:00 2001 From: Dan Nyanko Date: Mon, 24 Aug 2026 08:15:08 -0400 Subject: [PATCH] fix(tests): polyfill localStorage in jsdom test setup jsdom does not reliably expose localStorage across runtimes (e.g. Node 26's experimental built-in shadows it), causing projectsPersistence/projectsSlice tests to fail with 'localStorage is undefined'. Add a minimal in-memory shim guarded by typeof check so it only applies when absent, keeping behavior identical on environments where jsdom already provides it. --- src/test/setup.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) 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();