install
source · Clone the upstream repo
git clone https://github.com/ComeOnOliver/skillshub
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/pproenca/dot-skills/vitest" ~/.claude/skills/comeonoliver-skillshub-vitest-85bdb6 && rm -rf "$T"
manifest:
skills/pproenca/dot-skills/vitest/SKILL.mdsource content
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 positivesasync-await-assertions
- Return promises from test functionsasync-return-promises
- Use fake timers for time-dependent codeasync-fake-timers
- Use vi.waitFor for async conditionsasync-waitfor-polling
- Use test context expect in concurrent testsasync-concurrent-expect
- Await user events to avoid act warningsasync-act-wrapper
- Test async error handling properlyasync-error-handling
2. Test Setup & Isolation (CRITICAL)
- Clean up state in afterEach hookssetup-beforeeach-cleanup
- Restore mocks after each testsetup-restore-mocks
- Avoid shared mutable state between testssetup-avoid-shared-state
- Use beforeAll for expensive one-time setupsetup-beforeall-expensive
- Reset modules when testing module statesetup-reset-modules
- Use test factories for complex test datasetup-test-factories
3. Mocking Patterns (HIGH)
- Understand vi.mock hoisting behaviormock-vi-mock-hoisting
- Choose vi.spyOn vs vi.mock appropriatelymock-spyon-vs-mock
- Use mockImplementation for dynamic mocksmock-implementation-not-value
- Use MSW for network request mockingmock-msw-network
- Avoid over-mockingmock-avoid-overmocking
- Maintain type safety in mocksmock-type-safety
- Clear mock state between testsmock-clear-between-tests
4. Performance (HIGH)
- Choose the right pool for performanceperf-pool-selection
- Disable test isolation when safeperf-disable-isolation
- Use happy-dom over jsdom when possibleperf-happy-dom
- Use sharding for CI parallelizationperf-sharding
- Use run mode in CI environmentsperf-run-mode-ci
- Use bail for fast failure in CIperf-bail-fast-fail
5. Snapshot Testing (MEDIUM)
- Prefer inline snapshots for small valuessnap-inline-over-file
- Avoid large snapshotssnap-avoid-large
- Ensure stable snapshot serializationsnap-stable-serialization
- Review snapshot updates before committingsnap-review-updates
- Name snapshot tests descriptivelysnap-describe-intent
6. Environment (MEDIUM)
- Override environment per file when neededenv-per-file-override
- Use setup files for global configurationenv-setup-files
- Configure globals consistentlyenv-globals-config
- Mock browser APIs not available in test environmentenv-browser-api-mocking
7. Assertions (LOW-MEDIUM)
- Use specific matchers over generic onesassert-specific-matchers
- Test edge cases and boundariesassert-edge-cases
- Test one concept per testassert-one-assertion-concept
- Use expect.assertions for async testsassert-expect-assertions
- Choose toBe vs toEqual correctlyassert-toequal-vs-tobe
8. Test Organization (LOW)
- Colocate test files with source filesorg-file-colocation
- Use describe blocks for logical groupingorg-describe-nesting
- Write descriptive test namesorg-test-naming
- Use skip and only appropriatelyorg-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
skilltest-tdd - For API mocking with MSW, see
skilltest-msw - For TypeScript testing patterns, see
skilltypescript
Full Compiled Document
For the complete guide with all rules expanded:
AGENTS.md