Vitest Testing

Configure Vitest unit and component tests for Vite projects with jsdom and mocking.

Updated Feb 25, 2026
One-click install
npx skills add https://github.com/futuretea/x-project --skill vitest-testing-futuretea
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Vitest Testing
Source: https://github.com/futuretea/x-project/tree/main/.claude/skills/vitest-testing
Command: npx skills add https://github.com/futuretea/x-project --skill vitest-testing-futuretea

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Vitest Testing removes friction from setting up, writing, and running fast unit and component tests in Vite-based JavaScript and TypeScript projects, reducing flaky tests and slow feedback loops.

Core Features & Use Cases

  • Configuration: Example vitest.config.ts and setup files for jsdom and coverage reporting.
  • Mocking & Spies: Patterns for vi.fn, vi.mock, and vi.spyOn to isolate units and external dependencies.
  • Component Testing: Guidance for Testing Library with React components, async flows, and user-event interactions.
  • Timers & Fetch: Techniques for fake timers and mocking global.fetch in unit tests.
  • Use Case: Add reliable unit and component tests to a Vite React app to validate business logic, UI behavior, and API error handling before CI runs.

Quick Start

Install Vitest and Testing Library, add a vitest.config.ts with environment set to jsdom and a setup file, then write focused .test.tsx files using vi.mock and Testing Library assertions.

Frequently Asked Questions about Vitest Testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I configure Vitest with jsdom for React component testing?

Setting up Vitest testing with jsdom requires a vitest.config.ts file with the environment set to jsdom and a setup file. This configuration provides the DOM APIs required by Testing Library to render and assert React components.

What is the best way to mock fetch requests and timers in Vitest?

The best way to mock fetch and timers in Vitest is using vi.mock for global.fetch and fake timers. These mocking APIs isolate units and external dependencies, ensuring deterministic test outcomes without waiting for real network or delay.

How do I use vi.spyOn and vi.fn to isolate dependencies in unit tests?

Use vi.fn to create mock functions and vi.spyOn to track or override existing module methods in Vitest. These APIs isolate external dependencies and verify interactions, ensuring tests focus purely on the target logic.

Does Vitest work with Testing Library for async React component flows?

Vitest works seamlessly with Testing Library for React component testing, providing guidance on async flows and user-event interactions. This combination validates UI behavior and API error handling within jsdom environments.

How to setup test coverage reporting in a Vite JavaScript project?

Test coverage reporting in a Vite project is enabled by configuring coverage options in your vitest.config.ts file. Vitest generates reports validating business logic and UI behavior before CI runs.

Why are my Vitest unit tests slow or flaky in local development?

Slow or flaky Vitest unit tests often result from unmocked network calls or real timers. Utilizing vi.mock for global.fetch and fake timers isolates units, removing external dependencies to ensure fast, deterministic feedback loops.