Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

test: share the jsdom localStorage fallback across workspaces (MUL-8592) (#9924)

Jiayuan Zhangb7c353c844ae
diff --git a/AGENTS.md b/AGENTS.md
index 212ef9c3d2..515799d15c 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -112,6 +112,7 @@ Workspace-scoped queries filter by `workspace_id`; membership gates access and `
- Tests live beside their implementation: shared logic in core, shared components in views, platform wiring in apps, E2E in `e2e/`, Go tests in server. Do not test shared behavior in app suites.
- Give each behavior one canonical test layer: helper tests own parsing/state matrices; component tests cover wiring, accessibility, happy paths, and named regressions. Prefer a failing regression test before behavioral fixes.
- DOM-free `.test.ts` files start with `// @vitest-environment node`; do not use it if it would silently switch the code under test to an SSR path.
+- Every workspace with jsdom suites loads `@multica/vitest-setup/web-storage` in its Vitest setup; do not add per-package `localStorage` shims.
- Views tests must not mock `next/*` or `react-router-dom`. Mock stores with their Zustand callable shape plus `getState`; mock API calls at `@multica/core/api`.
- E2E setup/teardown uses `TestApiClient`.
- DB-backed Go tests use `server/internal/testutil` fixtures (`dbfx.Issue`, `dbfx.Task`, `dbfx.Insert`) and `testutil.Call(h, req).Want(status).JSON(&out)`. Keep product assertions and case-specific diagnostics in the test, not fixture helpers.
diff --git a/apps/desktop/package.json b/apps/desktop/package.json
index 0c0cfd44b1..86f06659a1 100644
--- a/apps/desktop/package.json
+++ b/apps/desktop/package.json
@@ -61,6 +61,7 @@
"@electron-toolkit/tsconfig": "^2.0.0",
"@multica/eslint-config": "workspace:*",
"@multica/tsconfig": "workspace:*",
+ "@multica/vitest-setup": "workspace:*",
"@tailwindcss/vite": "^4",
"@testing-library/jest-dom": "catalog:",
"@testing-library/react": "catalog:",
diff --git a/apps/desktop/test/setup.ts b/apps/desktop/test/setup.ts
index 7231809125..9a43c1d79c 100644
--- a/apps/desktop/test/setup.ts
+++ b/apps/desktop/test/setup.ts
@@ -1,46 +1,10 @@
import "@testing-library/jest-dom/vitest";
-
-function createMemoryStorage(): Storage {
- const values = new Map<string, string>();
-
- return {
- get length() {
- return values.size;
- },
- clear: () => values.clear(),
- getItem: (key: string) => values.get(key) ?? null,
- key: (index: number) => Array.from(values.keys())[index] ?? null,
- removeItem: (key: string) => {
- values.delete(key);
- },
- setItem: (key: string, value: string) => {
- values.set(key, value);
- },
- };
-}
+import "@multica/vitest-setup/web-storage";
// Everything below patches gaps in jsdom. Pure-logic suites opt out of jsdom
// with `// @vitest-environment node` and share this file, so there is no DOM to
// patch there — bail out rather than guard each stub.
if (typeof window !== "undefined") {
- const localStorageIsUsable =
- typeof globalThis.localStorage?.getItem === "function" &&
- typeof globalThis.localStorage?.setItem === "function" &&
- typeof globalThis.localStorage?.removeItem === "function" &&
- typeof globalThis.localStorage?.clear === "function";
-
- if (!localStorageIsUsable) {
- const storage = createMemoryStorage();
- Object.defineProperty(globalThis, "localStorage", {
- configurable: true,
- value: storage,
- });
- Object.defineProperty(window, "localStorage", {
- configurable: true,
- value: storage,
- });
- }
-
// jsdom doesn't provide matchMedia; the sidebar's compact breakpoint and
// auto-collapse band both read it. Nothing matches, so a shell mounted here
// renders at its full desktop width. Mirrors packages/views/test/setup.ts.
diff --git a/apps/web/package.json b/apps/web/package.json
index 3e890a60ae..d873434480 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -30,6 +30,7 @@
},
"devDependencies": {
"@multica/eslint-config": "workspace:*",
+ "@multica/vitest-setup": "workspace:*",
"@tailwindcss/postcss": "catalog:",
"postcss": "catalog:",
"@testing-library/jest-dom": "catalog:",
diff --git a/apps/web/test/setup.ts b/apps/web/test/setup.ts
index 27b58c14cf..9dcc4dbb2d 100644
--- a/apps/web/test/setup.ts
+++ b/apps/web/test/setup.ts
@@ -1,5 +1,5 @@
import "@testing-library/jest-dom/vitest";
-import { vi } from "vitest";
+import "@multica/vitest-setup/web-storage";
// Everything below patches gaps in jsdom. Pure-logic suites opt out of jsdom
// with `// @vitest-environment node` and share this file, so there is no DOM to
@@ -19,35 +19,4 @@ if (typeof window !== "undefined") {
if (typeof document.elementFromPoint !== "function") {
document.elementFromPoint = () => null;
}
-
- // jsdom 29 / Node.js 22+ may not provide a proper Web Storage API.
- // Create a proper localStorage mock if methods are missing.
- if (
- typeof globalThis.localStorage === "undefined" ||
- typeof globalThis.localStorage.getItem !== "function"
- ) {
- const store: Record<string, string> = {};
- const localStorageMock = {
- getItem: vi.fn((key: string) => store[key] ?? null),
- setItem: vi.fn((key: string, value: string) => {
- store[key] = value;
- }),
- removeItem: vi.fn((key: string) => {
- delete store[key];
- }),
- clear: vi.fn(() => {
- for (const key of Object.keys(store)) {
- delete store[key];
- }
- }),
- get length() {
- return Object.keys(store).length;
- },
- key: vi.fn((index: number) => Object.keys(store)[index] ?? null),
- };
- Object.defineProperty(globalThis, "localStorage", {
- value: localStorageMock,
- writable: true,
- });
- }
}
diff --git a/knip.jsonc b/knip.jsonc
index a19014ef17..6af908e176 100644
--- a/knip.jsonc
+++ b/knip.jsonc
@@ -74,6 +74,7 @@
},
"packages/eslint-config": {},
- "packages/tsconfig": {}
+ "packages/tsconfig": {},
+ "packages/vitest-setup": {}
}
}
diff --git a/packages/core/package.json b/packages/core/package.json
index bb5abf90e5..b06572e337 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -199,6 +199,7 @@
"devDependencies": {
"@multica/eslint-config": "workspace:*",
"@multica/tsconfig": "workspace:*",
+ "@multica/vitest-setup": "workspace:*",
"@testing-library/react": "catalog:",
"@types/react": "catalog:",
"@typescript/native": "catalog:",
diff --git a/packages/core/vitest.config.ts b/packages/core/vitest.config.ts
index 947172e8a8..82e6b14767 100644
--- a/packages/core/vitest.config.ts
+++ b/packages/core/vitest.config.ts
@@ -3,6 +3,7 @@ import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
globals: true,
+ setupFiles: ["@multica/vitest-setup/web-storage"],
include: ["**/*.test.{ts,tsx}"],
passWithNoTests: true,
},
diff --git a/packages/ui/package.json b/packages/ui/package.json
index abac217f1f..c3e1b7949d 100644
--- a/packages/ui/package.json
+++ b/packages/ui/package.json
@@ -67,6 +67,7 @@
"devDependencies": {
"@multica/eslint-config": "workspace:*",
"@multica/tsconfig": "workspace:*",
+ "@multica/vitest-setup": "workspace:*",
"@testing-library/jest-dom": "catalog:",
"@testing-library/react": "catalog:",
"@types/linkify-it": "^5.0.0",
diff --git a/packages/ui/test/setup.ts b/packages/ui/test/setup.ts
index 921e26398e..24caf964a7 100644
--- a/packages/ui/test/setup.ts
+++ b/packages/ui/test/setup.ts
@@ -1,5 +1,6 @@
import { afterEach } from "vitest";
import "@testing-library/jest-dom/vitest";
+import "@multica/vitest-setup/web-storage";
// jsdom ships neither class. use-scroll-fade observes both.
const roInstances: ResizeObserverStub[] = [];
diff --git a/packages/views/package.json b/packages/views/package.json
index 11f0b5ed30..f5ea02fab9 100644
--- a/packages/views/package.json
+++ b/packages/views/package.json
@@ -139,6 +139,7 @@
"@multica/eslint-config": "workspace:*",
"@multica/plugin-sdk": "workspace:*",
"@multica/tsconfig": "workspace:*",
+ "@multica/vitest-setup": "workspace:*",
"@testing-library/jest-dom": "catalog:",
"@testing-library/react": "catalog:",
"@testing-library/user-event": "catalog:",
diff --git a/packages/views/test/setup.ts b/packages/views/test/setup.ts
index 1712edb6f2..7561b75b3a 100644
--- a/packages/views/test/setup.ts
+++ b/packages/views/test/setup.ts
@@ -1,40 +1,10 @@
import "@testing-library/jest-dom/vitest";
-
-function createMemoryStorage(): Storage {
- const values = new Map<string, string>();
-
- return {
- get length() {
- return values.size;
- },
- clear: () => values.clear(),
- getItem: (key: string) => values.get(key) ?? null,
- key: (index: number) => Array.from(values.keys())[index] ?? null,
- removeItem: (key: string) => {
- values.delete(key);
- },
- setItem: (key: string, value: string) => {
- values.set(key, value);
- },
- };
-}
+import "@multica/vitest-setup/web-storage";
// Everything below patches gaps in jsdom. Pure-logic suites opt out of jsdom
// with `// @vitest-environment node` and share this file, so there is no DOM to
// patch there — bail out rather than guard each stub.
if (typeof window !== "undefined") {
- if (typeof globalThis.localStorage?.clear !== "function") {
- const storage = createMemoryStorage();
- Object.defineProperty(globalThis, "localStorage", {
- configurable: true,
- value: storage,
- });
- Object.defineProperty(window, "localStorage", {
- configurable: true,
- value: storage,
- });
- }
-
// jsdom doesn't provide matchMedia; useIsMobile() relies on it.
if (typeof window.matchMedia !== "function") {
window.matchMedia = (query: string) =>
diff --git a/packages/vitest-setup/package.json b/packages/vitest-setup/package.json
new file mode 100644
index 0000000000..3aa02ad36b
--- /dev/null
+++ b/packages/vitest-setup/package.json
@@ -0,0 +1,9 @@
+{
+ "name": "@multica/vitest-setup",
+ "version": "0.0.0",
+ "private": true,
+ "type": "module",
+ "exports": {
+ "./web-storage": "./web-storage.ts"
+ }
+}
diff --git a/packages/vitest-setup/web-storage.ts b/packages/vitest-setup/web-storage.ts
new file mode 100644
index 0000000000..a48287b5cf
--- /dev/null
+++ b/packages/vitest-setup/web-storage.ts
@@ -0,0 +1,46 @@
+// Node 25+ enables Web Storage by default. Without a valid
+// `--localstorage-file` its global `localStorage` has no methods, and it
+// shadows the working one jsdom provides. Swap in an in-memory Storage so jsdom
+// suites behave the same on every Node version `engines` allows.
+
+function createMemoryStorage(): Storage {
+ const values = new Map<string, string>();
+
+ return {
+ get length() {
+ return values.size;
+ },
+ clear: () => values.clear(),
+ getItem: (key: string) => values.get(key) ?? null,
+ key: (index: number) => Array.from(values.keys())[index] ?? null,
+ removeItem: (key: string) => {
+ values.delete(key);
+ },
+ setItem: (key: string, value: string) => {
+ values.set(key, String(value));
+ },
+ };
+}
+
+// Suites on the node environment have no DOM storage to stand in for.
+if (typeof window !== "undefined") {
+ const localStorageIsUsable =
+ typeof globalThis.localStorage?.getItem === "function" &&
+ typeof globalThis.localStorage?.setItem === "function" &&
+ typeof globalThis.localStorage?.removeItem === "function" &&
+ typeof globalThis.localStorage?.clear === "function";
+
+ if (!localStorageIsUsable) {
+ const storage = createMemoryStorage();
+ Object.defineProperty(globalThis, "localStorage", {
+ configurable: true,
+ value: storage,
+ });
+ Object.defineProperty(window, "localStorage", {
+ configurable: true,
+ value: storage,
+ });
+ }
+}
+
+export {};
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 135948fdb7..dff0541b06 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -231,6 +231,9 @@ importers:
'@multica/tsconfig':
specifier: workspace:*
version: link:../../packages/tsconfig
+ '@multica/vitest-setup':
+ specifier: workspace:*
+ version: link:../../packages/vitest-setup
'@tailwindcss/vite':
specifier: ^4
@@ -594,6 +597,9 @@ importers:
'@multica/eslint-config':
specifier: workspace:*
version: link:../../packages/eslint-config
+ '@multica/vitest-setup':
+ specifier: workspace:*
+ version: link:../../packages/vitest-setup
'@tailwindcss/postcss':
specifier: 'catalog:'
version: 4.2.2
@@ -667,6 +673,9 @@ importers:
'@multica/tsconfig':
specifier: workspace:*
version: link:../tsconfig
+ '@multica/vitest-setup':
+ specifier: workspace:*
+ version: link:../vitest-setup
'@testing-library/react':
specifier: 'catalog:'
@@ -838,6 +847,9 @@ importers:
'@multica/tsconfig':
specifier: workspace:*
version: link:../tsconfig
+ '@multica/vitest-setup':
+ specifier: workspace:*
+ version: link:../vitest-setup
'@testing-library/jest-dom':
specifier: 'catalog:'
version: 6.9.1
@@ -1082,6 +1094,9 @@ importers:
'@multica/tsconfig':
specifier: workspace:*
version: link:../tsconfig
+ '@multica/vitest-setup':
+ specifier: workspace:*
+ version: link:../vitest-setup
'@testing-library/jest-dom':
specifier: 'catalog:'
version: 6.9.1
@@ -1128,6 +1143,8 @@ importers:
specifier: 'catalog:'
+ packages/vitest-setup: {}
+
packages: