vitest
Original:🇺🇸 English
Translated
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.
2installs
Sourcepproenca/dot-skills
Added on
NPX Install
npx skill4agent add pproenca/dot-skills vitestTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Vitest Best Practices
Comprehensive performance optimization and best practices guide for Vitest testing framework. Contains 44 rules across 8 categories, prioritized by impact to guide test writing, refactoring, and code review.
When to Apply
Reference these guidelines when:
- Writing new Vitest tests
- Debugging flaky or slow tests
- Setting up test configuration
- Reviewing test code in PRs
- Migrating from Jest to Vitest
- Optimizing CI/CD test performance
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Async Patterns | CRITICAL | |
| 2 | Test Setup & Isolation | CRITICAL | |
| 3 | Mocking Patterns | HIGH | |
| 4 | Performance | HIGH | |
| 5 | Snapshot Testing | MEDIUM | |
| 6 | Environment | MEDIUM | |
| 7 | Assertions | LOW-MEDIUM | |
| 8 | Test Organization | LOW | |
Quick Reference
1. Async Patterns (CRITICAL)
- - Await async assertions to prevent false positives
async-await-assertions - - Return promises from test functions
async-return-promises - - Use fake timers for time-dependent code
async-fake-timers - - Use vi.waitFor for async conditions
async-waitfor-polling - - Use test context expect in concurrent tests
async-concurrent-expect - - Await user events to avoid act warnings
async-act-wrapper - - Test async error handling properly
async-error-handling
2. Test Setup & Isolation (CRITICAL)
- - Clean up state in afterEach hooks
setup-beforeeach-cleanup - - Restore mocks after each test
setup-restore-mocks - - Avoid shared mutable state between tests
setup-avoid-shared-state - - Use beforeAll for expensive one-time setup
setup-beforeall-expensive - - Reset modules when testing module state
setup-reset-modules - - Use test factories for complex test data
setup-test-factories
3. Mocking Patterns (HIGH)
- - Understand vi.mock hoisting behavior
mock-vi-mock-hoisting - - Choose vi.spyOn vs vi.mock appropriately
mock-spyon-vs-mock - - Use mockImplementation for dynamic mocks
mock-implementation-not-value - - Use MSW for network request mocking
mock-msw-network - - Avoid over-mocking
mock-avoid-overmocking - - Maintain type safety in mocks
mock-type-safety - - Clear mock state between tests
mock-clear-between-tests
4. Performance (HIGH)
- - Choose the right pool for performance
perf-pool-selection - - Disable test isolation when safe
perf-disable-isolation - - Use happy-dom over jsdom when possible
perf-happy-dom - - Use sharding for CI parallelization
perf-sharding - - Use run mode in CI environments
perf-run-mode-ci - - Use bail for fast failure in CI
perf-bail-fast-fail
5. Snapshot Testing (MEDIUM)
- - Prefer inline snapshots for small values
snap-inline-over-file - - Avoid large snapshots
snap-avoid-large - - Ensure stable snapshot serialization
snap-stable-serialization - - Review snapshot updates before committing
snap-review-updates - - Name snapshot tests descriptively
snap-describe-intent
6. Environment (MEDIUM)
- - Override environment per file when needed
env-per-file-override - - Use setup files for global configuration
env-setup-files - - Configure globals consistently
env-globals-config - - Mock browser APIs not available in test environment
env-browser-api-mocking
7. Assertions (LOW-MEDIUM)
- - Use specific matchers over generic ones
assert-specific-matchers - - Test edge cases and boundaries
assert-edge-cases - - Test one concept per test
assert-one-assertion-concept - - Use expect.assertions for async tests
assert-expect-assertions - - Choose toBe vs toEqual correctly
assert-toequal-vs-tobe
8. Test Organization (LOW)
- - Colocate test files with source files
org-file-colocation - - Use describe blocks for logical grouping
org-describe-nesting - - Write descriptive test names
org-test-naming - - Use skip and only appropriately
org-test-skip-only
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
- async-await-assertions - Example rule file
- mock-vi-mock-hoisting - Example rule file
Related Skills
- For TDD methodology, see skill
test-tdd - For API mocking with MSW, see skill
test-msw - For TypeScript testing patterns, see skill
typescript
Full Compiled Document
For the complete guide with all rules expanded:
AGENTS.md