test(setup): npx vitest가 npm scope 환경변수 없이 시작한다 (1101/15-05)
문제 `npx vitest run tests/unit/ci-workflow-generation.test.ts`는 79건이 "CI runner npm scope environment invalid"로 실패한다(415건 중). npx와 npm을 거친 IDE 실행기는 npm_config_prefix·_userconfig·_globalconfig를 넣는다. CI 게이트 preflight(withCiGatePreflight)는 실제 runner에서 이 변수들을 거절하는데(옳은 동작), 테스트가 그 preflight를 프로세스 안에서 부르거나 자식으로 띄우면서 실행기의 process.env를 그대로 물려받는다. scripts/run-vitest.ts만 그중 일부를 지워서 pnpm 경로만 통과한다. 원인은 timeout이 아니라 이 환경변수다. 고른 방법 - vitest.config.ts가 NODE_ENV를 고정하는 자리에서 npm scope 변수를 지운다. config는 worker를 띄우기 전에 메인 프로세스에서 평가되므로 worker와 그 자식 프로세스 모두 깨끗한 환경으로 시작한다. 어느 실행기로 돌리든, 어느 테스트 파일이든 한 곳에서 막는다. - 지울 이름은 preflight가 쓰는 것과 같은 판정 (package-script-graph.ts의 isNpmScopeEnvironmentName, 기존 private 함수를 export)으로 고른다. 목록이 둘로 갈라지지 않는다. - ci-workflow-generation.test.ts에 "테스트 프로세스에 npm scope 변수가 없다"는 테스트를 더했다. 버린 대안 - 해당 테스트 파일의 beforeEach/afterEach에서 지우고 되돌리기(리뷰의 최소 방향 1): 같은 preflight를 띄우는 다른 파일이 생기면 다시 깨진다. - 실행 함수가 환경을 인자로 받게 하기: 자식 프로세스는 여전히 process.env를 물려받아 실패한다. - run-vitest.ts의 하드코딩 목록 정리: 이번 수정으로 필요 없어지지만, product index 생성 자식에도 쓰이고 동작이 같아 건드리지 않았다. 위험·한계 - 실제 게이트 실행기의 npm scope 거절은 그대로다. 테스트 안에서 일부러 변수를 넣는 기존 테스트 3건도 그대로 통과한다. - tests/unit 밖(component, integration 등)은 npx로 돌려 보지 않았다. 검증 - RED: npx vitest run tests/unit/ci-workflow-generation.test.ts -t "npm-launched runner" 실패 "expected [ 'npm_config_userconfig', …(2) ] to deeply equal []". 수정 전 같은 파일 79 failed / 336 passed - GREEN: npx vitest run tests/unit/ci-workflow-generation.test.ts 416 passed - npx vitest run tests/unit: 171파일 2,401 passed - tsc(node, test), eslint 바뀐 파일 통과 Refs: docs/reviews/2026-11-01-maturity-review/15-performance.md 15-05
This commit is contained in:
@@ -245,7 +245,7 @@ export function validateNpmScopeEnvironment(
|
||||
environment: Readonly<Record<string, string | undefined>>,
|
||||
): string[] {
|
||||
return Object.keys(environment)
|
||||
.filter((name) => npmDispatchScopeEnvironmentNames.has(name.toLowerCase()))
|
||||
.filter(isNpmScopeEnvironmentName)
|
||||
.map((name) => `npm scope environment is not allowed: ${name}`);
|
||||
}
|
||||
|
||||
@@ -744,7 +744,7 @@ function parseEnvironmentAssignmentName(token: string): string | null {
|
||||
return equals > 0 ? token.slice(0, equals) : null;
|
||||
}
|
||||
|
||||
function isNpmScopeEnvironmentName(name: string): boolean {
|
||||
export function isNpmScopeEnvironmentName(name: string): boolean {
|
||||
return npmDispatchScopeEnvironmentNames.has(name.toLowerCase());
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ import {
|
||||
type CiWorkflowFileSystem,
|
||||
} from "../../scripts/generate-ci-workflow.ts";
|
||||
import {
|
||||
isNpmScopeEnvironmentName,
|
||||
validateInstallScriptPolicy,
|
||||
validatePackageScriptGraph,
|
||||
} from "../../scripts/lib/package-script-graph.ts";
|
||||
@@ -743,6 +744,14 @@ describe("CI gate contract", () => {
|
||||
},
|
||||
);
|
||||
|
||||
// 1101/15-05. `npx vitest` and npm-launched IDE runners hand the test
|
||||
// process npm_config_prefix/userconfig/globalconfig. The preflight above
|
||||
// rightly refuses them, so 79 tests here failed for the caller's
|
||||
// environment, not for the code. vitest.config.ts drops them.
|
||||
it("starts without the npm scope variables an npm-launched runner sets", () => {
|
||||
expect(Object.keys(process.env).filter(isNpmScopeEnvironmentName)).toEqual([]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["npm run nested --ignore-scripts=false", "nested"],
|
||||
["npm test -- --ignore-scripts=true", "test"],
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
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];
|
||||
}
|
||||
|
||||
// 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`
|
||||
|
||||
Reference in New Issue
Block a user