perf(tests): vitest setup이 DOM이 있는 파일에만 Testing Library를 불러온다

문제: 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회.
This commit is contained in:
DongHyeonka
2026-11-12 00:05:25 +09:00
parent 92abfd2089
commit 1f09c1cc88
+21 -12
View File
@@ -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();