import { configDefaults, defineConfig } from "vitest/config"; import { isNpmScopeEnvironmentName } from "./scripts/lib/package-script-graph.ts"; // Tests must not inherit a caller's production React/runtime selection. // Package scripts also launch Vitest through scripts/run-vitest.ts, which owns // NODE_ENV before the Vitest process starts. Keeping the config deterministic // protects direct `vitest` invocations as well. process.env.NODE_ENV = "test"; // 1101/15-05. `npx vitest` and npm-launched IDE runners set npm_config_prefix, // _userconfig and _globalconfig. The CI gate preflight refuses those in a real // runner (scripts/contracts/ci-gates.ts), so tests that run it, in process or // as a child, failed for the caller's environment. Tests start without them. for (const name of Object.keys(process.env)) { if (isNpmScopeEnvironmentName(name)) delete process.env[name]; } // CI sets VITE_BUILD_ID and VITE_COMMIT_SHA for the whole workflow // (.gitea/workflows/quality-gates.yml). Vite builds them into // `import.meta.env`, so a test that composes the app against its local-build // fixture release saw CONFIG_BUILD_MISMATCH in CI and passed everywhere else. // Tests start with the local build identity. delete process.env["VITE_BUILD_ID"]; delete process.env["VITE_COMMIT_SHA"]; // tests/system needs CI-runner host facilities (see docs/testing/taxonomy.md). // It runs only when a caller names it, as `test:system` does; a bare `vitest` // run leaves it out. const systemTestsRequested = process.argv.some((argument) => argument.replaceAll("\\", "/").replace(/^\.\//u, "").startsWith("tests/system"), ); export default defineConfig({ test: { globals: false, // 10-04. Vitest refuses `.only` only when CI is set; locally it quietly // drops the rest of the file. Refuse it everywhere. Skipped and todo // tests are failed by scripts/lib/no-skipped-tests-reporter.ts. allowOnly: false, setupFiles: ["./tests/setup.ts"], restoreMocks: true, clearMocks: true, mockReset: true, testTimeout: 10_000, // Playwright specs (*.spec.ts) share tests/ but belong to Playwright. include: ["tests/**/*.test.{ts,tsx}"], exclude: [ ...configDefaults.exclude, ".tmp/**", // A git worktree inside the repository is a different checkout of a // different branch. Running its tests against this checkout's config // reports failures that belong to neither. ".worktrees/**", "tests/fixtures/v8-coverage-counter-semantics/**", ...(systemTestsRequested ? [] : ["tests/system/**"]), ], coverage: { provider: "v8", reportsDirectory: "artifacts/tests/coverage", reporter: ["text", "json-summary", "lcov"], include: ["src/**/*.{ts,tsx}"], exclude: ["**/*.d.ts", "**/*.stories.*"], }, }, });