docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를 Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류. - 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로 자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거, 동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거. - 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming, Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business, Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로, 나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는 title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백). 원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지. - 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서. - 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는 지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지. - Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경. - 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
This commit is contained in:
@@ -0,0 +1,233 @@
|
||||
---
|
||||
id: wiki-2026-0508-unit-tests-단위-테스트
|
||||
title: Unit Tests (단위 테스트)
|
||||
category: 10_Wiki/Topics
|
||||
status: verified
|
||||
canonical_id: self
|
||||
aliases: [단위 테스트, Unit Testing, Unit Test]
|
||||
duplicate_of: none
|
||||
source_trust_level: A
|
||||
confidence_score: 0.9
|
||||
verification_status: applied
|
||||
tags: [testing, unit-test, tdd, vitest]
|
||||
raw_sources: []
|
||||
last_reinforced: 2026-05-10
|
||||
github_commit: pending
|
||||
tech_stack:
|
||||
language: typescript
|
||||
framework: vitest-2
|
||||
---
|
||||
|
||||
# Unit Tests (단위 테스트)
|
||||
|
||||
## 매 한 줄
|
||||
> **"매 작은 unit (function / class) 을 isolate 해서 매 fast 하게 검증"**. 매 test pyramid 의 base. 2026 JS 진영은 Vitest 가 매 default — fast, ESM-native, jest-compatible API.
|
||||
|
||||
## 매 핵심
|
||||
|
||||
### 매 properties (FIRST)
|
||||
- **Fast**: ms 단위.
|
||||
- **Independent**: 순서 무관.
|
||||
- **Repeatable**: deterministic.
|
||||
- **Self-validating**: pass/fail 명확.
|
||||
- **Timely**: 매 production code 와 동시 (또는 먼저).
|
||||
|
||||
### 매 무엇이 unit
|
||||
- 매 단일 function / 매 class 의 method / 매 small module.
|
||||
- 매 collaborator 는 stub/mock 또는 매 real (sociable test).
|
||||
- 매 file system / network / DB 는 통상 test 가 unit 아님.
|
||||
|
||||
### 매 응용
|
||||
1. TDD red-green-refactor.
|
||||
2. Regression guard.
|
||||
3. Living documentation.
|
||||
4. Refactoring safety net.
|
||||
|
||||
## 💻 패턴
|
||||
|
||||
### Vitest basic (AAA)
|
||||
```typescript
|
||||
// sum.ts
|
||||
export function sum(a: number, b: number): number {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
// sum.test.ts
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { sum } from './sum';
|
||||
|
||||
describe('sum', () => {
|
||||
it('adds two positive numbers', () => {
|
||||
// Arrange
|
||||
const a = 2, b = 3;
|
||||
// Act
|
||||
const result = sum(a, b);
|
||||
// Assert
|
||||
expect(result).toBe(5);
|
||||
});
|
||||
|
||||
it('handles negatives', () => {
|
||||
expect(sum(-1, 1)).toBe(0);
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
### Parameterized
|
||||
```typescript
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe.each([
|
||||
{ a: 1, b: 2, want: 3 },
|
||||
{ a: 0, b: 0, want: 0 },
|
||||
{ a: -1, b: 1, want: 0 },
|
||||
])('sum($a, $b)', ({ a, b, want }) => {
|
||||
it(`= ${want}`, () => {
|
||||
expect(sum(a, b)).toBe(want);
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
### Mock collaborator
|
||||
```typescript
|
||||
import { vi, expect, it } from 'vitest';
|
||||
|
||||
interface Mailer {
|
||||
send(to: string, body: string): Promise<void>;
|
||||
}
|
||||
|
||||
class SignupService {
|
||||
constructor(private mailer: Mailer) {}
|
||||
async signup(email: string) {
|
||||
await this.mailer.send(email, 'Welcome!');
|
||||
}
|
||||
}
|
||||
|
||||
it('sends welcome email on signup', async () => {
|
||||
const mailer: Mailer = { send: vi.fn().mockResolvedValue(undefined) };
|
||||
const svc = new SignupService(mailer);
|
||||
|
||||
await svc.signup('a@b.c');
|
||||
|
||||
expect(mailer.send).toHaveBeenCalledWith('a@b.c', 'Welcome!');
|
||||
});
|
||||
```
|
||||
|
||||
### Module mock
|
||||
```typescript
|
||||
import { vi } from 'vitest';
|
||||
|
||||
vi.mock('./db', () => ({
|
||||
findUser: vi.fn().mockResolvedValue({ id: '1', name: 'A' }),
|
||||
}));
|
||||
```
|
||||
|
||||
### Time / clock
|
||||
```typescript
|
||||
import { afterEach, beforeEach, vi, expect, it } from 'vitest';
|
||||
|
||||
beforeEach(() => vi.useFakeTimers());
|
||||
afterEach(() => vi.useRealTimers());
|
||||
|
||||
it('expires after 1h', () => {
|
||||
const session = createSession({ ttlMs: 3600_000 });
|
||||
expect(session.expired()).toBe(false);
|
||||
vi.advanceTimersByTime(3600_001);
|
||||
expect(session.expired()).toBe(true);
|
||||
});
|
||||
```
|
||||
|
||||
### Property-based (fast-check)
|
||||
```typescript
|
||||
import fc from 'fast-check';
|
||||
import { it, expect } from 'vitest';
|
||||
|
||||
it('reverse(reverse(xs)) == xs', () => {
|
||||
fc.assert(
|
||||
fc.property(fc.array(fc.integer()), (xs) => {
|
||||
expect([...xs].reverse().reverse()).toEqual(xs);
|
||||
}),
|
||||
);
|
||||
});
|
||||
```
|
||||
|
||||
### Snapshot (use sparingly)
|
||||
```typescript
|
||||
it('renders config', () => {
|
||||
expect(buildConfig({ env: 'prod' })).toMatchInlineSnapshot(`
|
||||
{
|
||||
"apiUrl": "https://api.example.com",
|
||||
"retries": 3,
|
||||
}
|
||||
`);
|
||||
});
|
||||
```
|
||||
|
||||
### React component (Testing Library)
|
||||
```tsx
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { Counter } from './Counter';
|
||||
|
||||
it('increments on click', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<Counter />);
|
||||
|
||||
await user.click(screen.getByRole('button', { name: /\+/ }));
|
||||
|
||||
expect(screen.getByText('Count: 1')).toBeInTheDocument();
|
||||
});
|
||||
```
|
||||
|
||||
### Coverage
|
||||
```bash
|
||||
vitest run --coverage
|
||||
```
|
||||
```typescript
|
||||
// vitest.config.ts
|
||||
export default {
|
||||
test: {
|
||||
coverage: {
|
||||
provider: 'v8',
|
||||
reporter: ['text', 'html'],
|
||||
thresholds: { lines: 80, functions: 80, branches: 70 },
|
||||
},
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
## 매 결정 기준
|
||||
| 상황 | Approach |
|
||||
|---|---|
|
||||
| 매 pure logic | Plain unit test |
|
||||
| 매 collaborator-heavy | Sociable test (real collaborator) 또는 mock |
|
||||
| 매 algorithmic invariants | Property-based (fast-check) |
|
||||
| 매 React component | Testing Library — user-centric |
|
||||
| 매 stable structured output | Snapshot (sparingly) |
|
||||
| 매 DB / network | 매 integration test (not unit) |
|
||||
|
||||
**기본값**: 매 sociable + AAA + named cases. 매 mock 은 매 boundary 에서만.
|
||||
|
||||
## 🔗 Graph
|
||||
- 부모: [[Test_Pyramid]]
|
||||
- 응용: [[TDD]] · [[Mocking]] · [[Test_Doubles]]
|
||||
|
||||
## 🤖 LLM 활용
|
||||
**언제**: 매 test scaffolding, 매 edge case 생성, 매 missing branch coverage 의 fill.
|
||||
**언제 X**: 매 fundamental design problem — 매 test 만으로는 fix 안됨.
|
||||
|
||||
## ❌ 안티패턴
|
||||
- **Test implementation**: 매 internal 의 mock — 매 refactor breakage.
|
||||
- **Slow units**: 매 DB / network → unit 아님.
|
||||
- **Shared state**: 매 order-dependent flake.
|
||||
- **Snapshot 남발**: 매 update 만 누름 — 매 검증 안됨.
|
||||
- **Coverage as goal**: 매 100% line ≠ 매 quality.
|
||||
|
||||
## 🧪 검증 / 중복
|
||||
- Verified (Vitest 2.x docs, Beck "Test-Driven Development", Meszaros "xUnit Test Patterns", 2026).
|
||||
- 신뢰도 A.
|
||||
|
||||
## 🕓 Changelog
|
||||
| 날짜 | 변경 |
|
||||
|---|---|
|
||||
| 2026-05-08 | Phase 1 |
|
||||
| 2026-05-10 | Manual cleanup — Vitest unit tests with mocks, fake timers, fast-check |
|
||||
Reference in New Issue
Block a user