|
| 1 | +// Node 25 ships an experimental built-in `localStorage` global (gated on the |
| 2 | +// `--localstorage-file` flag, but the accessor exists unconditionally). When |
| 3 | +// vitest boots the jsdom environment, jsdom defines its own `localStorage` on |
| 4 | +// the synthetic `window`, but Node's global accessor still wins on |
| 5 | +// `globalThis.localStorage` AND — because vitest's jsdom integration aliases |
| 6 | +// `window` to `globalThis` — also on `window.localStorage`. The result is that |
| 7 | +// `window.localStorage` resolves to Node's stub object which has no `clear`, |
| 8 | +// `setItem`, `removeItem`, etc., breaking every test that touches localStorage. |
| 9 | +// |
| 10 | +// This setup file installs a proper in-memory Storage implementation on both |
| 11 | +// `globalThis` and `window` BEFORE any test code runs. The shim is a plain |
| 12 | +// object (not a class) so `vi.spyOn(window.localStorage, "getItem")` works — |
| 13 | +// vitest needs the methods to be own properties on the spied target. |
| 14 | +// |
| 15 | +// We re-install the shim in `beforeEach` so a test that did |
| 16 | +// `vi.restoreAllMocks()` (which restores spied methods) still sees the shim's |
| 17 | +// methods, and so each test starts with a fresh empty store. |
| 18 | + |
| 19 | +import { beforeEach } from "vitest"; |
| 20 | + |
| 21 | +function createStorageShim(): Storage { |
| 22 | + const store = new Map<string, string>(); |
| 23 | + const shim = { |
| 24 | + get length() { |
| 25 | + return store.size; |
| 26 | + }, |
| 27 | + key(index: number): string | null { |
| 28 | + return Array.from(store.keys())[index] ?? null; |
| 29 | + }, |
| 30 | + getItem(key: string): string | null { |
| 31 | + return store.has(key) ? store.get(key)! : null; |
| 32 | + }, |
| 33 | + setItem(key: string, value: string): void { |
| 34 | + store.set(String(key), String(value)); |
| 35 | + }, |
| 36 | + removeItem(key: string): void { |
| 37 | + store.delete(key); |
| 38 | + }, |
| 39 | + clear(): void { |
| 40 | + store.clear(); |
| 41 | + }, |
| 42 | + } as Storage; |
| 43 | + return shim; |
| 44 | +} |
| 45 | + |
| 46 | +function installLocalStorageShim(): void { |
| 47 | + const shim = createStorageShim(); |
| 48 | + // Override the Node 25 global accessor (and any jsdom accessor) with a |
| 49 | + // plain data property pointing at our shim. `configurable: true` so a |
| 50 | + // subsequent install can replace it. |
| 51 | + Object.defineProperty(globalThis, "localStorage", { |
| 52 | + value: shim, |
| 53 | + writable: true, |
| 54 | + configurable: true, |
| 55 | + enumerable: true, |
| 56 | + }); |
| 57 | + if (typeof window !== "undefined" && window !== (globalThis as unknown)) { |
| 58 | + Object.defineProperty(window, "localStorage", { |
| 59 | + value: shim, |
| 60 | + writable: true, |
| 61 | + configurable: true, |
| 62 | + enumerable: true, |
| 63 | + }); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Install once at module load so any top-level code in test files (e.g. |
| 68 | +// imports that read localStorage on init) sees the shim. |
| 69 | +installLocalStorageShim(); |
| 70 | + |
| 71 | +// Re-install before each test so `vi.restoreAllMocks()` from a prior test |
| 72 | +// can't leave behind spied/replaced methods, and each test starts with an |
| 73 | +// empty store. |
| 74 | +beforeEach(() => { |
| 75 | + installLocalStorageShim(); |
| 76 | +}); |
0 commit comments