testing-patterns
應用 Vitest 測試模式
متاح أيضًا من: sickn33,0xDarkMatter,0xDarkMatter,ChrisWiles
為新功能、代理和 API 路由編寫測試需要一致的模式。此技能提供可直接使用的 Vitest 模式,用於單元測試、代理測試以及帶有 Supabase 模擬的 API 測試。
تنزيل ZIP المهارة
رفع في Claude
اذهب إلى Settings → Capabilities → Skills → Upload skill
فعّل وابدأ الاستخدام
اختبرها
استخدام "testing-patterns". Write a Vitest test for UserService that tests user creation and validates input.
النتيجة المتوقعة:
- 為 UserService 建立 describe 區塊
- 使用 vi.clearAllMocks() 設置 beforeEach
- 使用有效輸入測試使用者建立
- 測試無效輸入的錯誤處理
- 模擬 Supabase from() 和 insert() 方法
استخدام "testing-patterns". Create a test for an AI agent that processes tasks with a mocked database.
النتيجة المتوقعة:
- 使用 vi.mock 模擬 Supabase 客戶端
- 定義包含必要欄位的測試任務
- 呼叫 agent.processTask(task)
- 斷言結果已定義且有效
- 驗證模擬已正確呼叫
استخدام "testing-patterns". Test an API endpoint that requires authentication.
النتيجة المتوقعة:
- 建立不含 workspace_id 的 NextRequest
- 呼叫 GET 處理程序
- 預期 400 狀態碼
- 驗證回應中的錯誤訊息
- 使用有效憑證測試成功案例
التدقيق الأمني
آمنPure documentation skill containing only markdown documentation with TypeScript test examples. No executable code, scripts, network calls, or filesystem access beyond reading the file. All 31 static findings are false positives caused by the scanner misinterpreting markdown code block syntax (backticks) and JSON metadata fields as security-sensitive patterns.
عوامل الخطر
🌐 الوصول إلى الشبكة (2)
📁 الوصول إلى نظام الملفات (1)
⚙️ الأوامر الخارجية (9)
درجة الجودة
ماذا يمكنك بناءه
單元測試服務
使用標準的 describe-it-expect 模式為新服務和工具建立單元測試。
測試代理邏輯
使用模擬的 Supabase 客戶端為 AI 代理 processTask 方法編寫測試。
驗證 API 路由
測試 API 端點,包括身份驗證、驗證和回應處理。
جرّب هذه الموجهات
Write a Vitest unit test for MyService that tests data processing and error handling. Use beforeEach for setup and vi.clearAllMocks.
Write a Vitest test for MyAgent.processTask with mocked Supabase. Include test task with workspace_id, task_type, and payload.
Write a Vitest test for GET /api/my-endpoint that verifies workspace_id authentication is required. Expect 400 status on missing parameter.
Show me how to mock Supabase client in Vitest with mockFrom, mockSelect, mockEq functions that return test data.
أفضل الممارسات
- 在 beforeEach 中使用 vi.clearAllMocks() 來重置測試之間的模擬狀態
- 在 describe 區塊之前使用 vi.mock 在模組層級模擬 Supabase
- 對於 API 路由,首先測試身份驗證和驗證
- 維持所有測試套件 100% 通過率
تجنب
- 跳過使用 beforeEach 進行測試清理,導致測試污染
- 不模擬外部相依項,如 Supabase
- 編寫測試時沒有針對錯誤案例的斷言
- 不當地混合整合測試和單元測試模式