Lumen/tests/setup.ts

35 lines
1 KiB
TypeScript

/**
* Vitest setup — runs before every test file.
*
* Why: on Node >=22.4 the experimental native `localStorage` global shadows
* jsdom's implementation and is `undefined` unless `--localstorage-file` is
* passed, so jsdom-environment tests see `localStorage === undefined`
* (ExperimentalWarning at runtime). Install a minimal in-memory stand-in
* whenever the global is missing.
*/
class MemoryStorage implements Storage {
private map = new Map<string, string>();
get length(): number {
return this.map.size;
}
clear(): void {
this.map.clear();
}
getItem(key: string): string | null {
return this.map.get(key) ?? null;
}
key(index: number): string | null {
return [...this.map.keys()][index] ?? null;
}
removeItem(key: string): void {
this.map.delete(key);
}
setItem(key: string, value: string): void {
this.map.set(key, value);
}
}
const g = globalThis as unknown as { localStorage?: Storage };
if (typeof g.localStorage === "undefined") {
g.localStorage = new MemoryStorage();
}