Vibe Coding SDLC BiblePart 4 of 6

Testing & QA: Pastikan Kode Benar-Benar Bekerja

Kode yang "works di laptop saya" belum tentu works di production. Part 4 mengajarkan testing pyramid (unit 70%, integration 20%, E2E 10%), unit tests dengan Vitest, E2E tests dengan Playwright, AI-generated test suites, security scanning (npm audit, Snyk, gitleaks), performance testing (Lighthouse), dan CI integration — semua dengan Vibe Coding approach dimana AI generate 70-80% test boilerplate.

Maret 202640 menit bacaTesting • Vitest • Playwright • E2E • Security Scan • CI
1 2 3 4 5 6

Daftar Isi — Part 4: Testing & QA

  1. Testing Pyramid — Unit 70%, Integration 20%, E2E 10%
  2. Unit Tests (Vitest) — Test functions, validations, utils
  3. Integration Tests — Test API endpoints end-to-end
  4. E2E Tests (Playwright) — Simulate real user flows
  5. AI-Generated Tests — Generate 80% test boilerplate
  6. Security Scanning — npm audit, Snyk, gitleaks
  7. Performance Testing — Lighthouse, Core Web Vitals
  8. CI Integration — Auto-run tests on every PR
  9. Coverage Goals & Checklist
🏠

1. Testing Pyramid

Banyak test cepat di bawah (unit), sedikit test lambat di atas (E2E)

Testing pyramid adalah prinsip fundamental: tulis banyak unit tests (cepat, murah, 70% effort), beberapa integration tests (medium, 20%), dan sedikit E2E tests (lambat, mahal, 10%). Dalam Vibe Coding, AI bisa generate 70-80% test boilerplate — tapi Anda tetap harus define edge cases dan acceptance criteria yang bermakna.

LayerWhatToolSpeedQtyCoverage
UnitIndividual functions, hooks, utils, Zod schemasVitest<1s/test50-100+~70%
IntegrationAPI routes, DB operations, auth flowsVitest + Prisma mock1-5s/test20-40~20%
E2EFull user flows in real browserPlaywright5-30s/test5-15~10%
SecurityVulnerabilities, secrets, depsnpm audit, SnykVariesAutoContinuous
🧪

2. Unit Tests dengan Vitest

Test individual functions: validation rules, calculations, formatting, utils
tests/unit/task-validation.test.ts
import { describe, it, expect } from "vitest"; import { CreateTaskSchema } from "@/lib/schemas"; describe("CreateTaskSchema", () => { it("accepts valid task data", () => { const result = CreateTaskSchema.safeParse({ title: "Fix login bug", boardId: "550e8400-e29b-41d4-a716-446655440000", }); expect(result.success).toBe(true); }); it("rejects title shorter than 3 chars", () => { const result = CreateTaskSchema.safeParse({ title: "ab", boardId: "550e8400-e29b-41d4-a716-446655440000", }); expect(result.success).toBe(false); }); it("rejects missing boardId", () => { const result = CreateTaskSchema.safeParse({ title: "Valid" }); expect(result.success).toBe(false); }); it("rejects SQL injection in title", () => { // Zod catches invalid format, Prisma parameterizes queries const result = CreateTaskSchema.safeParse({ title: "'; DROP TABLE tasks; --", boardId: "550e8400-e29b-41d4-a716-446655440000", }); // Title passes Zod (it's a valid string >3 chars) // But Prisma ORM will parameterize it — NO injection! expect(result.success).toBe(true); }); }); // npx vitest run --reporter=verbose // 4 tests passed in 0.2s
🌐

3. E2E Tests dengan Playwright

Simulate real user: buka browser, klik, isi form, verifikasi hasil
tests/e2e/kanban.spec.ts
import { test, expect } from "@playwright/test"; test("user can create and move a task", async ({ page }) => { // 1. Login await page.goto("/login"); await page.fill("[name=email]", "test@example.com"); await page.fill("[name=password]", "testpass123"); await page.click("button[type=submit]"); await expect(page).toHaveURL("/dashboard"); // 2. Go to board await page.click("text=My Board"); await expect(page.getByText("TODO")).toBeVisible(); // 3. Create task await page.click("[data-testid=add-task-btn]"); await page.fill("[name=title]", "E2E Test Task"); await page.click("[data-testid=submit-task]"); // 4. Verify task visible in TODO column await expect(page.getByText("E2E Test Task")).toBeVisible(); }); // npx playwright test --headed (see browser) // npx playwright test (headless, for CI)
🤖

4. AI-Generated Tests

Prompt AI untuk generate comprehensive test suites dari existing code

Prompt Pattern: Generate Tests

"Read file src/server/tasks.ts and generate comprehensive tests: (1) Unit tests for every Zod validation rule, (2) Integration tests for CRUD operations, (3) Edge cases: empty string, max length, SQL injection attempt, unauthorized user, non-existent IDs, (4) Use Vitest + Prisma mock, (5) AAA pattern (Arrange-Act-Assert), (6) Generate at least 15 test cases." — AI generates 15-20 tests in seconds. Review manually: AI sometimes misses business-specific edge cases.

🔒

5. Security Scanning

Automated security checks: dependencies, secrets, runtime vulnerabilities
ToolWhat It ChecksCommandFrequency
npm auditKnown vulns in npm packagesnpm audit --productionEvery PR
SnykDeep dependency scan + fix suggestionssnyk testDaily
gitleaksSecrets accidentally committedgitleaks detectEvery commit (hook)
ESLint securityCommon code security issueseslint-plugin-securityEvery save
LighthouseBest practices, SEO, accessibilitynpx lighthouse URLPre-release

6. Coverage Goals & CI Integration

Target coverage dan auto-run tests di setiap PR
MetricTargetTool
Line coverage>80%vitest --coverage
Branch coverage>70%vitest --coverage
E2E critical paths100% (all user stories)Playwright
Security vulnerabilities0 high/criticalnpm audit + Snyk
Secrets in repo0gitleaks
Lighthouse score>90 (all categories)Lighthouse CI
CI pipelineAll above auto-run on every PRGitHub Actions
SDLC
Tech Review Desk — Vibe Coding SDLC Bible
Sumber: Vitest docs, Playwright docs, OWASP Testing Guide, Snyk docs, Google Lighthouse.
rominur@gmail.com  •  t.me/Jekardah_AI