From 0e1302ef45af41770647d84969fdeaf3824f59ef Mon Sep 17 00:00:00 2001 From: chanwoo7 Date: Sun, 4 Oct 2026 23:49:03 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=EC=95=B1=20=EB=B6=80=ED=8C=85=EC=9D=B4?= =?UTF-8?q?=20zod=20=ED=95=9C=EA=B5=AD=EC=96=B4=20=EB=AC=B8=EA=B5=AC?= =?UTF-8?q?=EB=A5=BC=20=EC=84=A4=EC=B9=98=ED=95=98=EB=8A=94=EC=A7=80=20?= =?UTF-8?q?=EA=B3=A0=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 테스트 setup과 zod-locale spec이 installZodKorean()을 직접 불러서, main.tsx에서 호출이 빠져도 잡는 테스트가 없었음 - 부팅을 함수로 떼어 그 함수를 검사하면 main.tsx의 호출 자체는 여전히 비어 있으므로, main.tsx를 직접 import하는 spec으로 바꿈 - react-dom/client의 createRoot만 stub, #root를 둔 뒤 zod를 영어 로캘·customError 없음으로 되돌리고 import - 메시지 없는 규칙(min(1))이 한국어 문구를 내고, #root로 render가 1회 불리는지 확인. 끝나면 한국어 설정을 다시 설치 - 회귀 1건(main.tsx의 installZodKorean() 줄을 지우면 영어 문구가 나와 실패 확인) --- src/main.spec.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/main.spec.ts diff --git a/src/main.spec.ts b/src/main.spec.ts new file mode 100644 index 0000000..61e73ee --- /dev/null +++ b/src/main.spec.ts @@ -0,0 +1,33 @@ +import { z } from 'zod'; + +import { installZodKorean } from '@/shared/lib/zod-locale'; + +const { createRoot, render } = vi.hoisted(() => { + const render = vi.fn(); + return { render, createRoot: vi.fn(() => ({ render })) }; +}); +vi.mock('react-dom/client', () => ({ createRoot })); + +const emptyMessage = () => z.string().min(1).safeParse('').error?.issues[0]?.message; + +/** 테스트 setup도 한국어 문구를 설치하므로, main.tsx에서 설치가 빠져도 다른 spec은 모른다. */ +describe('앱 부팅(main.tsx)', () => { + afterEach(() => { + installZodKorean(); + document.getElementById('root')?.remove(); + }); + + it('zod 한국어 문구를 설치하고 #root에 앱을 한 번 렌더한다', async () => { + z.config({ ...z.locales.en(), customError: undefined }); + expect(emptyMessage()).not.toBe('값을 입력해 주세요.'); + const root = document.createElement('div'); + root.id = 'root'; + document.body.append(root); + + await import('./main'); + + expect(emptyMessage()).toBe('값을 입력해 주세요.'); + expect(createRoot).toHaveBeenCalledWith(root); + expect(render).toHaveBeenCalledTimes(1); + }); +});