Files
2nd/10_Wiki/Topics/Coding/Testing_Snapshot_Patterns.md
T
2026-05-09 21:08:02 +09:00

3.3 KiB

id, title, category, status, source_trust_level, verification_status, created_at, updated_at, tags, tech_stack, applied_in, aliases
id title category status source_trust_level verification_status created_at updated_at tags tech_stack applied_in aliases
testing-snapshot-patterns Snapshot Test — 적절한 사용처 Coding draft B conceptual 2026-05-09 2026-05-09
testing
snapshot
jest
vibe-coding
language applicable_to
Jest / Vitest
Web
Backend
toMatchSnapshot
inline snapshot
golden file

Snapshot Test

의도적으로 사용하면 강력하지만 무지성으로 쓰면 "test 가 그냥 현재 출력을 freeze" 함. 변경 → snapshot 갱신 → 의미 없는 통과. diff 검토 문화 가 핵심.

📖 핵심 개념

  • 첫 실행: 출력 기록.
  • 이후: 기록과 다르면 fail.
  • Inline snapshot: test 파일에 직접 (변화 즉시 보임).
  • File snapshot: 별도 .snap 파일.

💻 코드 패턴

React 컴포넌트 — 보수적 사용

import { render } from '@testing-library/react';

test('Header renders title', () => {
  const { container } = render(<Header title="Hello" />);
  expect(container).toMatchSnapshot();
});

⚠️ 큰 컴포넌트는 변화 잦음. inline + 핵심만 권장.

Inline snapshot — diff 즉시 visible

import { test, expect } from 'vitest';

test('format duration', () => {
  expect(formatDuration(95_000)).toMatchInlineSnapshot(`"1m 35s"`);
  // jest --ci 면 fail / 로컬 --updateSnapshot 으로 갱신
});

Serializer — 핵심만 비교

expect.addSnapshotSerializer({
  test: (val) => val instanceof Date,
  print: () => '"<Date>"',
});

// timestamp 같이 매번 다른 값은 mask
test('user object', () => {
  expect(makeUser()).toMatchSnapshot();
  // {"id": "<Date>", "createdAt": "<Date>", ...}
});

Property snapshot — 동적 값 마스킹

expect(user).toMatchSnapshot({
  id: expect.any(String),
  createdAt: expect.any(Date),
});

Golden file — 큰 출력

test('SQL builder generates expected query', async () => {
  const sql = build({ table: 'users', filters: { age: 18 } });
  await expect(sql).toMatchFileSnapshot('./golden/users.sql');
});

🤔 의사결정 기준

사용처 snapshot 적합
작은 안정적 컴포넌트 단 inline + 짧게
큰 페이지 컴포넌트 — assertion 으로 행위 검증
빌더 / 직렬화 결과 — golden file
API 응답 형식 — schema 검증 (zod) 권장
CSS-in-JS 출력 — 자주 바뀜
에러 메시지 텍스트 inline

안티패턴

  • -u 무지성: CI 빨갛다고 자동 update. 의미 없는 통과.
  • 거대 snapshot: diff 못 읽음. 작은 단위로 분할.
  • timestamp / random / uuid 안 마스킹: 매번 fail. property matcher.
  • snapshot only test: 비즈니스 행위 검증 0. assertion 결합.
  • inline 안 쓰고 매번 .snap 파일 열기: 흐름 깨짐. 작은 출력은 inline.
  • eslint-plugin-jest 의 no-large-snapshots 무시: 50줄 넘는 snapshot 은 검토 신호.
  • PR 에서 snapshot 변경 review 안 함: 사일런트 회귀.

🤖 LLM 활용 힌트

  • "snapshot 보단 명시적 assertion 우선. snapshot 은 builder/format/error 메시지 같은 안정 출력만" 명시.
  • 동적 값은 property matcher 로 마스킹.

🔗 관련 문서