Vitest testing framework patterns for test setup, async testing, mocking with vi.*, snapshots, and test performance (formerly test-vitest). This skill should be used when writing or debugging Vitest tests. This skill does NOT cover TDD methodology (use test-tdd skill), API mocking with MSW (use test-msw skill), or Jest-specific APIs.
A comprehensive testing best practices skill for Vitest, the next-generation testing framework powered by Vite.
This skill contains 44 rules across 8 categories, organized by impact level to help prioritize the most important patterns when writing and reviewing Vitest tests.
| Category | Impact | Rules | Description | |----------|--------|-------|-------------| | Async Patterns | CRITICAL | 7 | Prevent race conditions, false positives, and flaky async tests | | Test Setup & Isolation | CRITICAL | 6 | Ensure proper test isolation and cleanup | | Mocking Patterns | HIGH | 7 | Effective mocking strategies without over-mocking | | Performance | HIGH | 6 | Optimize test execution speed | | Snapshot Testing | MEDIUM | 5 | Maintainable snapshot practices | | Environment | MEDIUM | 4 | Proper environment configuration | | Assertions | LOW-MEDIUM | 5 | Effective assertion patterns | | Test Organization | LOW | 4 | File structure and naming conventions |
vitest/
├── SKILL.md # Entry point with quick reference
├── AGENTS.md # Compiled comprehensive guide
├── metadata.json # Version and references
├── README.md # This file
├── references/
│ ├── _sections.md # Category definitions
│ ├── async-*.md # Async pattern rules
│ ├── setup-*.md # Setup and isolation rules
│ ├── mock-*.md # Mocking pattern rules
│ ├── perf-*.md # Performance rules
│ ├── snap-*.md # Snapshot rules
│ ├── env-*.md # Environment rules
│ ├── assert-*.md # Assertion rules
│ └── org-*.md # Organization rules
└── assets/templates/
└── _template.md # Rule template
Reference SKILL.md for quick navigation or AGENTS.md for the complete compiled guide.
Browse individual rule files in the references/ directory for detailed explanations and code examples.
// Always await async assertions
await expect(asyncFn()).rejects.toThrow('Error')
// Use fake timers for time-dependent code
vi.useFakeTimers()
vi.advanceTimersByTime(1000)
// Use vi.waitFor instead of arbitrary timeouts
await vi.waitFor(() => expect(element).toBeVisible())
// Clean up after each test
afterEach(() => {
vi.restoreAllMocks()
vi.useRealTimers()
})
// Avoid shared mutable state
let testData: User[]
beforeEach(() => {
testData = [createTestUser()] // Fresh for each test
})
// Prefer vi.spyOn for targeted mocking
vi.spyOn(api, 'fetchUser').mockResolvedValue(testUser)
// Use MSW for network mocking
const server = setupServer(
http.get('/api/users', () => HttpResponse.json([testUser]))
)
// vitest.config.ts
export default defineConfig({
test: {
pool: 'threads', // Faster than forks
isolate: false, // If tests are properly isolated
environment: 'happy-dom', // Faster than jsdom
},
})
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer