Files
AgentHub/client/vitest.config.ts
nntrivi2001 03a178a718 feat(dashboard): default to Analytics tab; fix(test): unbreak jsdom localStorage under Node 22+
Dashboard now opens on the Analytics tab for first-time visitors (localStorage
still wins once a user picks a tab). Also unbreaks the client test suite in
this environment: Node 22+'s built-in --experimental-webstorage localStorage
stub was shadowing jsdom's working one, failing every test that touches
localStorage. Disabled via a Node-major-gated poolOptions.forks.execArgv so
Node 20/21 test runs are unaffected.
2026-08-25 13:34:42 +07:00

56 lines
2.1 KiB
TypeScript

/**
* @file vitest.config.ts
* @description Vitest configuration for the client test suite — jsdom environment, React plugin, test globals, and the same build-time `__APP_VERSION__` injection as vite.config.ts so version-dependent components render identically under test.
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
// Mirror vite.config.ts: inject the repo-root project version as `__APP_VERSION__`
// so components that render it (e.g. the sidebar footer) behave the same in tests.
// Fail-safe resolution (root -> client -> placeholder) matches vite.config.ts.
function resolveAppVersion(): string {
for (const rel of ["../package.json", "package.json"]) {
try {
const { version } = JSON.parse(readFileSync(resolve(process.cwd(), rel), "utf8"));
if (version) return version as string;
} catch {
// Not found or unreadable at this path — try the next candidate.
}
}
return "0.0.0";
}
const APP_VERSION = resolveAppVersion();
// Node >=22 ships a built-in `localStorage` global (Web Storage API) that, without
// a configured `--localstorage-file`, is a non-functional stub (getItem/setItem
// undefined) — it shadows jsdom's own working localStorage before the jsdom
// environment can install it. Disabling it on those Node versions lets jsdom's
// localStorage apply instead; the flag doesn't exist on Node 20/21, so it's
// gated to avoid breaking test runs there.
const nodeMajor = Number(process.versions.node.split(".")[0]);
export default defineConfig({
plugins: [react()],
define: {
__APP_VERSION__: JSON.stringify(APP_VERSION),
},
test: {
environment: "jsdom",
environmentOptions: {
jsdom: {
url: "http://localhost/",
},
},
...(nodeMajor >= 22
? { poolOptions: { forks: { execArgv: ["--no-experimental-webstorage"] } } }
: {}),
setupFiles: ["./src/test-setup.ts"],
include: ["src/**/*.test.{ts,tsx}"],
css: false,
},
});