Test — Chạy và tạo tests

Chạy test suite, đo coverage, UI testing qua browser, và tự động generate tests cho code mới.

Cú pháp

/test                           # Chạy tất cả tests
/test ui https://localhost:3000 # UI testing trên browser
/test-gen                       # Generate tests tự động

Luồng hoạt động bên trong

/test — Chạy tests

1

Bước 1: Pre-flight Checks

Chạy typecheck (tsc --noEmit) + lint (eslint) để bắt lỗi cú pháp trước.
2

Bước 2: Execute Test Suites

Tự động detect test framework (Jest/Vitest/Mocha/pytest/go test) và chạy. Hỗ trợ: unit tests, integration tests, e2e tests.
3

Bước 3: Coverage Analysis

Đo coverage: lines, branches, functions, statements. So sánh với target (mặc định 80%).
4

Bước 4: Analyze Failures

Nếu có failing tests → phân tích root cause. Kích hoạt ck:debug nếu cần investigation sâu.
5

Bước 5: UI Tests (nếu có frontend)

Kích hoạt ck:chrome-devtools cho:
  • Screenshots, responsive checks
  • Accessibility audits
  • Console error collection
6

Bước 6: Report

Tổng hợp kết quả thành QA report cấu trúc.

/test-gen — Generate tests

1

Bước 1: Scan codebase

Tìm files chưa có test (so sánh *.ts với *.spec.ts/*.test.ts).
2

Bước 2: Phân tích code

Đọc functions, classes, exports. Xác định input/output types.
3

Bước 3: Generate test cases

Tạo tests bao gồm: happy path + edge cases + error scenarios. Dựa trên analysis code thực tế, không generic.
4

Bước 4: Verify

Chạy tests mới tạo để xác nhận PASS.

Ví dụ thực tế

TEST RESULTS
├── Typecheck:  PASS
├── Lint:       PASS
├── Tests:      142 total | 138 pass | 4 fail
├── Coverage:
│   ├── Lines:      84%
│   ├── Branches:   72%
│   ├── Functions:  89%
│   └── Statements: 83%
└── Duration:   12.3s

FAILURES:
1. auth.service.spec.ts:45 — expect(token).toBeDefined() → undefined
   Root cause: Mock không trả token khi email valid
2. order.service.spec.ts:78 — timeout 5000ms exceeded
   Root cause: Database connection pool exhausted

So sánh: /test vs /test-gen vs test-generator skill

Tính năng/test/test-gentest-generator skill
Chạy tests hiện cóKhông (chỉ generate)Không
Generate tests mớiKhôngCó (proactive)
Coverage analysisCó (sau generate)Không
UI testingCó (browser)KhôngKhông
Kích hoạt khi nào/test/test-genTự động khi viết code mới
Fix failing testsPhân tích, suggestKhôngKhông

So sánh với chat trực tiếp

Tiêu chí/testChat trực tiếp
Pre-flightTự động typecheck + lint trướcPhải nhắc chạy thủ công
CoverageĐo và report tự độngPhải hỏi riêng
UI testingBrowser automation (screenshots, a11y)Không có khả năng
Framework detectionTự động detect Jest/Vitest/pytestPhải nói rõ framework
Failure analysisPhân tích root cause, gọi ck:debugChỉ đọc error message
Report formatStructured QA reportText tự do

Khi nào dùng / không dùng

DùngKhông dùng
Sau khi implement featureKhi muốn QA toàn diện (dùng /qa-full:full)
Pre-commit kiểm tra nhanhKhi cần test theo PRD (dùng /qa-full:check)
Đo coverage hiện tạiKhi cần verify GitHub issue (dùng /qa-full:verify-issue)
Generate tests cho code mớiKhi cần TDD workflow (dùng /qa-full:tdd)

Test Framework hỗ trợ

import { describe, it, expect } from 'vitest';
import { getUserById } from './user.service';

describe('getUserById', () => {
  it('trả về user khi id hợp lệ', async () => {
    const user = await getUserById('user-1');
    expect(user).toBeDefined();
    expect(user?.id).toBe('user-1');
  });

  it('trả về null khi id không tồn tại', async () => {
    const user = await getUserById('invalid');
    expect(user).toBeNull();
  });
});

Kết hợp với QA

Mục đíchLệnh DevLệnh QA
Chạy tests/test/qa-full:check
Generate tests/test-gen/qa-full:check (bước 5)
Fix tests/fix:test/qa-full:full (bước 5)
Coverage report/test/qa-full:audit

Coverage Thresholds

MetricTargetMinimum
Lines80%+60%
Branches75%+55%
Functions85%+65%
Statements80%+60%
  • KHÔNG BAO GIỜ bỏ qua failing tests — fix root cause, không dùng mocks/cheats/tricks
  • Tất cả critical paths phải có test coverage
  • Validate happy path VÀ error scenarios
  • Tests phải isolation — không phụ thuộc lẫn nhau
  • Tests phải deterministic và reproducible