From ravn-ai-toolkit
Guides Vitest test writing with mocking, MSW v2 HTTP mocking, snapshot testing, and test infrastructure. Use when writing or debugging Vitest tests.
How this skill is triggered — by the user, by Claude, or both
Slash command
/ravn-ai-toolkit:tech-vitestThis skill is limited to the following tools:
The summary Claude sees in its skill listing — used to decide when to auto-load this skill
Fast, Vite-powered testing framework with Jest-compatible API and native ESM. Vitest excels at:
Fast, Vite-powered testing framework with Jest-compatible API and native ESM. Vitest excels at:
vi.mock() for modules, MSW v2 for HTTP requests, fake timers for time-dependent codeCore challenge: test isolation. Vitest's hoisting rules for vi.mock() require vi.hoisted() for shared mock state. MSW v2 changes handler APIs. Snapshot updates must be intentional.
When setting up tests or adding test cases:
vi.mock() with hoisting for modules, MSW for HTTP, fake timers for timevi.hoisted() if mocks need variables from test scopebeforeAll(), reset in afterEach()vitest --watch with coverage checksTests are organized by concern:
vi.mock(), vi.hoisted(), partial mocking with importOriginalvi.fn() with return values, implementations, and call trackingvi.spyOn() to watch object methods without replacingsetupServer(), per-test overridesvi.useFakeTimers(), vi.setSystemTime(), timer advancementtoMatchSnapshot(), inline snapshots, edge casesbeforeEach, afterEach, fixtures, setup filesSee rules/ for implementation patterns and examples.
User: "Mock the API client in this Vitest test so it returns a fake response."
Expected behavior: Use tech-vitest guidance, apply vi.mock() with hoisting rules and MSW patterns.
User: "Set up a PostgreSQL database schema for user accounts."
Expected behavior: Do not prioritize tech-vitest; choose a more relevant skill or proceed without it.
Error: Placing vi.mock() inside a condition
Cause: vi.mock() is hoisted; conditions are ignored, leading to unexpected module state
Solution: Move mocks to file top; use vi.hoisted() for conditional mock state
Error: Updating snapshots without reviewing changes
Cause: Snapshots bypass assertions; unexpected changes hide bugs
Solution: Always review diffs in --ui mode before accepting with -u
Error: Cannot access 'mockData' before initialization
Cause: Variables defined outside vi.mock() are not accessible in the factory (hoisting)
Solution: Wrap in vi.hoisted() to define shared mock state at the top level
Error: Mock handler never invoked; real HTTP request fires
Cause: MSW server not started or wrong handler method (http vs graphql)
Solution: Call server.listen() in beforeAll(); check handler verb matches request
Error: Test passes locally but fails in CI
Cause: Fake timers not reset between tests; time drifts across suite
Solution: Call vi.useRealTimers() in afterEach() without fail
npx claudepluginhub ravnhq/ai-toolkitVitest testing framework conventions and practices. Invoke whenever task involves any interaction with Vitest — writing tests, configuring vitest.config.ts, mocking modules, debugging test failures, snapshots, coverage, or migrating from Jest.
Provides Vitest testing patterns: assertions, mocking with vi.fn/vi.mock, async testing, and verification gates for writing and running tests.
Provides Vitest testing patterns for unit tests, mocks, spies, browser mode testing, setup, configuration, core concepts, and code examples.