| name | vitest-patterns |
| description | Vitest is a modern testing framework designed for the Vite ecosystem, Use when this capability is needed. |
| metadata | {"author":"amnadtaowsoam"} |
Vitest Patterns
Skill Profile
(Select at least one profile to enable specific modules)
Overview
Vitest is a modern testing framework designed for the Vite ecosystem, but works with any project. It's 10-20x faster than Jest, with native TypeScript/ESM support and Jest-compatible API, making migration easy. This skill covers Vitest setup, basic testing, mocking, React component testing, async testing, snapshot testing, test utilities, and running tests.
Why This Matters
Vitest provides:
- Speed: 10-20x faster than Jest (Vite's transform pipeline)
- Native ESM/TypeScript: No transpilation config needed
- Jest Compatible: Same API, easy migration
- Watch Mode: Instant feedback with smart re-runs
- UI Mode: Visual test debugging interface
Core Concepts
- Configuration: Vite config integration
- Test Environment: jsdom, node, happy-dom
- Globals: describe, it, expect globally available
- Mocking: vi.fn(), vi.mock(), vi.spyOn()
- Snapshots: Snapshot testing for UI components
- Coverage: Built-in coverage with v8 or istanbul
- Watch Mode: Smart file watching and re-running
Inputs / Outputs / Contracts
- Inputs:
- <e.g., env vars, request payload, file paths, schema>
- Entry Conditions:
- <Pre-requisites: e.g., Repo initialized, DB running, specific branch checked out>
- Outputs:
- <e.g., artifacts (PR diff, docs, tests, dashboard JSON)>
- Artifacts Required (Deliverables):
- <e.g., Code Diff, Unit Tests, Migration Script, API Docs>
- Acceptance Evidence:
- <e.g., Test Report (screenshot/log), Benchmark Result, Security Scan Report>
- Success Criteria:
- <e.g., p95 < 300ms, coverage ≥ 80%>
Skill Composition
- Depends on: None
- Compatible with: None
- Conflicts with: None
- Related Skills: None
Quick Start
Assumptions
- Project uses Vite or compatible build tool
- TypeScript is configured
- Testing libraries are installed
Compatibility
- Node.js: 16+
- Vite: 4+
- TypeScript: 4.5+
- React: 16.8+
Test Scenario Matrix (QA Strategy)
| Type | Focus Area | Required Scenarios / Mocks |
|---|
| Unit | Core Logic | Must cover primary logic and at least 3 edge/error cases. Target minimum 80% coverage |
| Integration | DB / API | All external API calls or database connections must be mocked during unit tests |
| E2E | User Journey | Critical user flows to test |
| Performance | Latency / Load | Benchmark requirements |
| Security | Vuln / Auth | SAST/DAST or dependency audit |
| Frontend | UX / A11y | Accessibility checklist (WCAG), Performance Budget (Lighthouse score) |
Technical Guardrails & Security Threat Model
1. Security & Privacy (Threat Model)
- Top Threats: Injection attacks, authentication bypass, data exposure
2. Performance & Resources
3. Architecture & Scalability
4. Observability & Reliability
Agent Directives
- Always clean up mocks
- Use vi.clearAllMocks() between tests
- Test one thing per test
- Keep tests independent
- Use descriptive test names
- Review snapshots before committing
Definition of Done (DoD) Checklist
Anti-patterns
-
Not Cleaning Up Mocks
describe('Test', () => {
it('test', () => {
const mock = vi.fn();
});
});
describe('Test', () => {
afterEach(() => {
vi.clearAllMocks();
});
it('test', () => {
const mock = vi.fn();
});
});
-
Testing Implementation Details
it('should set internal state', () => {
expect(component._state).toBe('value');
});
it('should display value', () => {
expect(screen.getByText('value')).toBeInTheDocument();
});
Reference Links & Examples
- Internal documentation and examples
- Official documentation and best practices
- Community resources and discussions
Versioning & Changelog
- Version: 1.0.0
- Changelog:
- 2026-02-22: Initial version with complete template structure
Converted and distributed by TomeVault — claim your Tome and manage your conversions.