Files
clean-architecture-frontend…/vitest.config.ts
T
DongHyeonka af312deeca fix(ci): 테스트와 dev server가 CI의 빌드 식별값을 물려받지 않는다
문제
.gitea/workflows/quality-gates.yml은 workflow 전체 env에
VITE_BUILD_ID=gitea-<run>-<attempt>와 VITE_COMMIT_SHA를 넣는다. Vite는 이
값을 import.meta.env로 번들에 넣는다. 그래서 CI와 같은 env로 돌리면:
- vitest: 앱을 local-build fixture release로 조립하는 테스트가
  CONFIG_BUILD_MISMATCH로 실패한다(runtime-application,
  boot-unexpected-failure, runtime-composition-service-worker-telemetry,
  reference-production-vertical, 생성 기능 페이지 테스트). FE-GATE-005·006·
  007과 제거 드릴(FE-GATE-020)이 떨어진다.
- capability e2e: dev server는 public/config.json(BUILD_ID local-build)을
  내주는데 번들은 gitea-1-1이라, 페이지마다 rolling-release 불일치로 한 번
  다시 불러오고 그다음 실패 화면이 된다. page.evaluate가 "Execution context
  was destroyed"로 30건 실패한다(FE-GATE-008).
깨끗한 clone에서 merge 단계 14개를 CI env로 돌려(리뷰 조치 X4) 찾았다.
로컬에서는 이 값이 없어서 늘 통과한다. 1103/11-06과 같은 원인이다.

고른 방법
- vitest.config.ts가 npm scope 변수를 지우는 것과 같은 자리에서
  VITE_BUILD_ID·VITE_COMMIT_SHA를 지운다. 테스트는 로컬 빌드 식별값으로
  시작한다.
- dev server를 띄우는 Playwright 설정(capabilities, dev)은 webServer env로
  VITE_BUILD_ID=local-build, VITE_COMMIT_SHA=local을 준다. 그 dev server가
  내주는 config.json과 같은 값이다.
- build·preview를 쓰는 e2e·visual은 바꾸지 않았다. 빌드가 같은 값으로
  config.json을 만들므로 맞는다.
- 버린 대안: 테스트마다 기대값을 env에서 읽기(11-06 방식). 같은 모양의
  fixture가 여러 파일에 있고, 다음 테스트가 또 같은 실수를 한다.

위험·한계
- vitest 안에서 CI 빌드 식별값을 읽는 것을 검사하려는 테스트는 값을 직접
  넣어야 한다(지금은 없다).
- CI=true와 러너 이미지 조건은 재현하지 않았다.

검증
- VITE_BUILD_ID=gitea-1-1, VITE_COMMIT_SHA=<HEAD>로: test:unit 2,878,
  test:component 270, test:integration 189, test:reference-feature 97,
  test:recipes 77 통과. browser-files capability spec chromium 3건 통과
  (수정 전 3건 실패, env 없으면 통과).

Refs: docs/reviews/2026-11-03-maturity-review/README.md 13절 11-06
2026-11-05 00:13:49 +09:00

65 lines
2.8 KiB
TypeScript

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.*"],
},
},
});