From 22df1840d6f8fac0aa103282175dac01a4c94ff8 Mon Sep 17 00:00:00 2001 From: ethernet Date: Sat, 1 Aug 2026 21:43:47 -0400 Subject: [PATCH] fix(desktop): restore localStorage in jsdom tests under Node 26 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Node 26 defines its own `localStorage` accessor on the global object, which returns `undefined` unless the process was started with `--localstorage-file` (hence the "localStorage is not available because --localstorage-file was not provided" warning now printed by every worker). In the jsdom environment `globalThis` IS the window, so that accessor shadows jsdom's Storage and every `localStorage.getItem(...)` in a test throws "Cannot read properties of undefined". CI caught this on the Node 26 bump: `check:test:ui` failed with 22 errors across session.test.ts, terminals.test.ts, model-settings and onboarding stores — all storage-backed. Reproduced locally against nodejs_26 (12 failures in src/store/session.test.ts alone) before fixing. vitest.setup.ts now installs a real in-memory Storage on both globalThis and window when the global resolves to undefined, before any test module reads it. Guarded on `typeof === 'undefined'` so Node < 26 and any future runtime that provides a working Storage keep jsdom's own implementation. Verified under nodejs_26: the full `--project ui` lane is 378 files / 3268 tests green (was 22 failures). --- apps/desktop/vitest.setup.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/apps/desktop/vitest.setup.ts b/apps/desktop/vitest.setup.ts index a780ec2faa..5e83926d58 100644 --- a/apps/desktop/vitest.setup.ts +++ b/apps/desktop/vitest.setup.ts @@ -1,5 +1,33 @@ import { configure } from '@testing-library/react' +// Node 26 defines its own `localStorage` accessor on the global object, which +// returns `undefined` unless the process was started with --localstorage-file +// (it warns: "localStorage is not available because --localstorage-file was +// not provided"). In the jsdom environment `globalThis` IS the window, so that +// accessor shadows jsdom's Storage and every `localStorage.getItem(...)` in a +// test throws "Cannot read properties of undefined". Install a real in-memory +// Storage when the global resolves to nothing, before any test module reads it. +if (typeof (globalThis as any).localStorage === 'undefined') { + const store = new Map() + const storage: Storage = { + get length() { + return store.size + }, + key: (i: number) => [...store.keys()][i] ?? null, + getItem: (k: string) => store.get(String(k)) ?? null, + setItem: (k: string, v: string) => void store.set(String(k), String(v)), + removeItem: (k: string) => void store.delete(String(k)), + clear: () => store.clear(), + } + for (const target of [globalThis, (globalThis as any).window].filter(Boolean)) { + Object.defineProperty(target, 'localStorage', { + value: storage, + configurable: true, + writable: true, + }) + } +} + // React 19 + Testing Library 16: opt into the act environment so render(), // fireEvent(), and findBy* queries automatically flush state updates without // spurious "not wrapped in act(...)" warnings.