write-tests

โดย sentry

เขียนเทสตามข้อตกลงของโปรเจกต์ ใช้เมื่อเพิ่มเทสใหม่หรือแก้ไขเทสที่มีอยู่

npx skills add https://github.com/getsentry/abacus --skill write-tests

Write Tests Skill

Write tests using Vitest. Tests are colocated next to source files.

Structure

src/lib/utils.ts
src/lib/utils.test.ts        # colocated
src/app/api/stats/route.ts
src/app/api/stats/route.test.ts
src/test-utils/
├── setup.ts        # global setup (PGlite, MSW, auth mock)
├── msw-handlers.ts # external API mocks
└── auth.ts         # auth test helpers

Auth Testing

Auth is globally mocked. Use helpers to control auth state:

import { mockAuthenticated, mockUnauthenticated } from '@/test-utils/auth';

it('returns 401 when unauthenticated', async () => {
  await mockUnauthenticated();
  const response = await GET(new Request('http://localhost/api/foo'));
  expect(response.status).toBe(401);
});

it('returns data when authenticated', async () => {
  await mockAuthenticated(); // uses default test user
  // or with overrides:
  await mockAuthenticated({ email: 'custom@example.com' });

  const response = await GET(new Request('http://localhost/api/foo'));
  expect(response.status).toBe(200);
});

Default test user: test@example.com / Test User

Database Testing

Uses PGlite (in-memory PostgreSQL). No Docker required.

  • Schema pushed automatically on boot
  • Each test runs in a transaction that rolls back
  • Use insertUsageRecord from @/lib/queries to seed data
import { insertUsageRecord, getOverallStats } from '@/lib/queries';

beforeEach(async () => {
  await insertUsageRecord({
    date: '2025-01-01',
    email: 'user@example.com',
    tool: 'claude_code',
    model: 'sonnet-4',
    rawModel: 'claude-sonnet-4-20250514',
    inputTokens: 1000,
    outputTokens: 500,
    cacheWriteTokens: 0,
    cacheReadTokens: 0,
    cost: 0.01,
  });
});

Running Tests

pnpm test        # run all
pnpm test:watch  # watch mode

Key Rules

  1. Colocate tests next to source (foo.ts → foo.test.ts)
  2. Use mockAuthenticated()/mockUnauthenticated() for auth
  3. Use insertUsageRecord for seeding database
  4. External APIs mocked via MSW in src/test-utils/msw-handlers.ts

CRITICAL: Auth Tests Required

Every protected route MUST have an auth test. This is non-negotiable.

Session-Authenticated Routes

Routes using getSession() must verify 401 on unauthenticated requests:

it('returns 401 for unauthenticated requests', async () => {
  await mockUnauthenticated();
  const response = await GET(new Request('http://localhost/api/your-route'));
  expect(response.status).toBe(401);
});

Cron Routes

Routes using CRON_SECRET must verify auth:

beforeEach(() => {
  vi.stubEnv('CRON_SECRET', 'test-secret');
});

it('returns 401 without authorization header', async () => {
  const response = await GET(new Request('http://localhost/api/cron/your-route'));
  expect(response.status).toBe(401);
});

it('returns 401 with invalid authorization', async () => {
  const response = await GET(
    new Request('http://localhost/api/cron/your-route', {
      headers: { Authorization: 'Bearer wrong-secret' },
    })
  );
  expect(response.status).toBe(401);
});

Webhook Routes

Routes with signature verification must test invalid signatures:

it('returns 401 without signature', async () => {
  const response = await POST(
    new Request('http://localhost/api/webhooks/github', {
      method: 'POST',
      body: '{}',
    })
  );
  expect(response.status).toBe(401);
});

it('returns 401 with invalid signature', async () => {
  const response = await POST(
    new Request('http://localhost/api/webhooks/github', {
      method: 'POST',
      body: '{}',
      headers: { 'x-hub-signature-256': 'sha256=invalid' },
    })
  );
  expect(response.status).toBe(401);
});

When adding a new route, always ask: "What auth does this route require?" and add the corresponding auth test.

Skills เพิ่มเติมจาก sentry

add-ai-integration
sentry
เพิ่มการรวมผู้ให้บริการ AI ใหม่เข้าไปใน Sentry JavaScript SDK ใช้เมื่อมีส่วนร่วมในการสร้างเครื่องมือวัด AI ใหม่ (OpenAI, Anthropic, Vercel AI, LangChain ฯลฯ)…
add-cdn-bundle
sentry
สร้างชุด CDN ใหม่สำหรับแพ็กเกจเบราว์เซอร์ที่มีฟีเจอร์ที่ระบุ
agent-prompt
sentry
คู่มืออ้างอิงสำหรับการเขียนพรอมต์และสกิลของเอเจนต์อย่างมีประสิทธิภาพ ใช้เมื่อสร้างสกิลใหม่ ตรวจสอบคุณภาพของพรอมต์ หรือทำความเข้าใจพรอมต์ของ Warden…
agents-md
sentry
ทักษะนี้ควรใช้เมื่อผู้ใช้ขอให้ "สร้าง AGENTS.md", "อัปเดต AGENTS.md", "ดูแลเอกสารเอเจนต์", "ตั้งค่า CLAUDE.md" หรือต้องการให้เอเจนต์…
agents-md
sentry
สร้างและดูแลเอกสารเอเจนต์ที่สั้นกระชับ มีเนื้อหาสำคัญ ภายใต้ 60 บรรทัด บังคับใช้แนวปฏิบัติที่ดีที่สุดที่ผ่านการวิจัยสำหรับเอกสารที่เอเจนต์ใช้ คุณภาพของคำสั่งจะลดลงตามความยาว ต้องมีสามส่วนหลัก: Package Manager, File-Scoped Commands (test/lint/typecheck ต่อไฟล์), และ Commit Attribution พร้อมระบุตัวตนเอเจนต์ วิเคราะห์โครงสร้างโปรเจกต์ (ไฟล์ lock, ไฟล์กำหนดค่า linter, คำสั่ง CI, ตัวบ่งชี้ monorepo) เพื่อกำหนดสิ่งที่ควรอยู่ในไฟล์ ใช้เฉพาะหัวข้อและสัญลักษณ์แสดงหัวข้อย่อย อ้างอิง...
agents-md
sentry
ทักษะนี้ควรใช้เมื่อผู้ใช้ขอให้ "สร้าง AGENTS.md", "อัปเดต AGENTS.md", "ดูแลเอกสารเอเจนต์", "ตั้งค่า CLAUDE.md" หรือต้องการให้เอเจนต์…
ai-sdk
sentry
ai-sdk — ทักษะที่สามารถติดตั้งได้สำหรับเอเจนต์ AI เผยแพร่โดย getsentry/webvitals.com
analytics
sentry
เครื่องมือและค้นพบเหตุการณ์วิเคราะห์ใน UI ส่วนหน้าของ Sentry ใช้เมื่อเพิ่มการติดตามไปยังปุ่ม หน้า โมดอล หรือการโต้ตอบที่กำหนดเอง เมื่อกำหนดใหม่…