SKILL.md
唯讀
名稱
vitest
描述
在為 Vite 專案撰寫單元/整合測試時使用 - 設定 vitest.config.ts、使用 describe/it 撰寫測試套件、使用 vi.fn 和 vi.mock 建立 mock 實作、設定程式碼涵蓋率門檻,並平行執行測試
Vitest
Vite 原生測試框架,具有 Jest 相容的 API。
使用時機
- 為 Vite 專案撰寫單元/整合測試
- 測試 Vue/React/Svelte 元件
- Mock 模組、計時器或日期
- 執行並行/平行測試
- 使用 TypeScript 進行型別測試
快速開始
npm i -D vitest
// vitest.config.ts
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
globals: true,
environment: 'node', // 或使用 'jsdom' 進行 DOM 測試
},
})
// example.test.ts
import { describe, expect, it, vi } from 'vitest'
describe('math', () => {
it('adds numbers', () => {
expect(1 + 1).toBe(2)
})
})
參考檔案
| 任務 | 檔案 |
|---|---|
| 設定、CLI、專案 | config.md |
| test/describe、鉤子、fixtures | test-api.md |
| vi.fn、vi.mock、計時器、spies | mocking.md |
| expect、snapshots、涵蓋率、過濾 | utilities.md |
| 環境、型別測試、瀏覽器模式 | advanced.md |
載入檔案
根據你的任務考慮載入這些參考檔案:
- [ ] references/config.md - 如果設定 vitest.config.ts、CLI 或 workspace 專案
- [ ] references/test-api.md - 如果撰寫 test/describe 區塊、使用鉤子或測試 fixtures
- [ ] references/mocking.md - 如果 mock 模組、計時器、日期或使用 spies
- [ ] references/utilities.md - 如果撰寫斷言、snapshots 或設定涵蓋率
- [ ] references/advanced.md - 如果設定測試環境、型別測試或瀏覽器模式
不要一次載入所有檔案。 只載入與目前任務相關的檔案。
跨技能參考
- Vue 元件測試 → 使用
vue技能取得元件模式 - 函式庫測試 → 使用
ts-library技能取得函式庫模式 - Vite 設定 → 使用
vite技能取得共用設定






