From 1f09c1cc88eccea8218e88c60d0dfff9233b5305 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 12 Nov 2026 00:05:25 +0900 Subject: [PATCH] =?UTF-8?q?perf(tests):=20vitest=20setup=EC=9D=B4=20DOM?= =?UTF-8?q?=EC=9D=B4=20=EC=9E=88=EB=8A=94=20=ED=8C=8C=EC=9D=BC=EC=97=90?= =?UTF-8?q?=EB=A7=8C=20Testing=20Library=EB=A5=BC=20=EB=B6=88=EB=9F=AC?= =?UTF-8?q?=EC=98=A8=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: tests/setup.ts는 모든 시험 파일에 @testing-library/jest-dom과 @testing-library/react(그리고 react-dom)를 불러온다. tests/unit 215개 파일 중 DOM(jsdom)을 쓰는 것은 3개뿐인데, Node 환경 파일마다 이 import가 돈다. runner의 test:unit에서 setup이 누적 124~165초로, 시험 본문 다음으로 크다(run 80·81). 고른 방법: setup 파일이 `document`가 있을 때만 두 모듈을 불러오고 configure를 건다. afterEach 하나에서 cleanup → 실제 timer 복원 순서를 그대로 지킨다. 버린 대안: vitest projects로 node·jsdom 설정을 나누기. 같은 효과에 설정·명령 변경이 커서 뒤로 미뤘다. 위험·한계: Node 환경 파일에서 jest-dom matcher를 쓰면 이제 실패한다. 그런 파일은 없다(아래 검증의 unit·component 전부 통과로 확인). 검증: desktop 묶음(w2fe-p2) A·B 단계 — unit·component 전·후 각 2회. --- tests/setup.ts | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/tests/setup.ts b/tests/setup.ts index 1272638d..87feb00f 100644 --- a/tests/setup.ts +++ b/tests/setup.ts @@ -1,19 +1,28 @@ -import "@testing-library/jest-dom/vitest"; -import { cleanup, configure } from "@testing-library/react"; import { afterEach, vi } from "vitest"; -// findBy*/waitFor settle on the DOM change itself; their deadline only reports -// a hang. Routes render through React.lazy, and React 19 holds every Suspense -// reveal on a real 300ms timer (FALLBACK_THROTTLE_MS). A route entered through -// a context table and then a lazy page spends 600ms of the 1s default before -// any rendering work, so on a loaded host the default turns into a speed -// requirement. Fake timers cannot shorten it: Testing Library advances only -// Jest's fake timers, and MSW and user-event run on the real clock. Half the -// 10s test timeout still reports a real hang with the DOM dump. -configure({ asyncUtilTimeout: 5_000 }); +// Testing Library and its matchers need a DOM. Most test files run in Node +// (tests/unit has over 200, three of them with jsdom), and importing React DOM +// and Testing Library into every one of them was the largest part of the +// suite's setup time. They load only where the file's environment has a DOM. +async function loadDomTesting(): Promise<() => void> { + await import("@testing-library/jest-dom/vitest"); + const { cleanup, configure } = await import("@testing-library/react"); + // findBy*/waitFor settle on the DOM change itself; their deadline only reports + // a hang. Routes render through React.lazy, and React 19 holds every Suspense + // reveal on a real 300ms timer (FALLBACK_THROTTLE_MS). A route entered through + // a context table and then a lazy page spends 600ms of the 1s default before + // any rendering work, so on a loaded host the default turns into a speed + // requirement. Fake timers cannot shorten it: Testing Library advances only + // Jest's fake timers, and MSW and user-event run on the real clock. Half the + // 10s test timeout still reports a real hang with the DOM dump. + configure({ asyncUtilTimeout: 5_000 }); + return cleanup; +} + +const cleanup = typeof document === "undefined" ? undefined : await loadDomTesting(); afterEach(() => { - cleanup(); + cleanup?.(); // A test that fails before its own vi.useRealTimers() must not leave fake // timers to the next test in the file. vi.useRealTimers();