From e0c2a91d473ac7337d67a610d3e9877550fc2dd6 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Sun, 1 Nov 2026 22:38:36 +0900 Subject: [PATCH] =?UTF-8?q?test(setup):=20npx=20vitest=EA=B0=80=20npm=20sc?= =?UTF-8?q?ope=20=ED=99=98=EA=B2=BD=EB=B3=80=EC=88=98=20=EC=97=86=EC=9D=B4?= =?UTF-8?q?=20=EC=8B=9C=EC=9E=91=ED=95=9C=EB=8B=A4=20(1101/15-05)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제 `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 --- scripts/lib/package-script-graph.ts | 4 ++-- tests/unit/ci-workflow-generation.test.ts | 9 +++++++++ vitest.config.ts | 9 +++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/scripts/lib/package-script-graph.ts b/scripts/lib/package-script-graph.ts index bab18ab3..eff06d86 100644 --- a/scripts/lib/package-script-graph.ts +++ b/scripts/lib/package-script-graph.ts @@ -245,7 +245,7 @@ export function validateNpmScopeEnvironment( environment: Readonly>, ): 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()); } diff --git a/tests/unit/ci-workflow-generation.test.ts b/tests/unit/ci-workflow-generation.test.ts index efa9d001..8ba6e386 100644 --- a/tests/unit/ci-workflow-generation.test.ts +++ b/tests/unit/ci-workflow-generation.test.ts @@ -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"], diff --git a/vitest.config.ts b/vitest.config.ts index 9235ef63..d89eec90 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -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`