vitest

Validate React component behavior with Vitest and React Testing Library.

3|Updated Apr 5, 2023
One-click install
npx skills add https://github.com/mrp4sten/.dotfiles --skill vitest-mrp4sten
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: vitest
Source: https://github.com/mrp4sten/.dotfiles/tree/main/development/IA/opencode/skill/vitest
Command: npx skills add https://github.com/mrp4sten/.dotfiles --skill vitest-mrp4sten

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Vitest-based testing patterns with React Testing Library help teams write fast, expressive tests for React components, ensuring robust UI behavior and maintainable test suites.

Core Features & Use Cases

  • AAA structure: Use Given/When/Then blocks to improve readability and reliability.
  • RTL Queries guidance: Prefer accessible queries and follow recommended priority.
  • Mocking and spies: Use vi.fn, vi.spyOn, and controlled mocks for deterministic tests.
  • Async testing: Demonstrate findBy, waitFor, and proper asynchronous assertions.
  • Hooks and snapshots: Include hook tests and restrained snapshot usage for maintainability.

Quick Start

Run the vitest test suite in your React project to validate components and hooks.

Frequently Asked Questions about vitest

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

FAQPage Schema
How do I write reliable React component tests with Vitest and React Testing Library?

Vitest React component testing uses React Testing Library queries and AAA patterns to validate component behavior. It structures tests with describe blocks, accessible queries, and isolated assertions to ensure reliable frontend UI testing.

What's the best way to mock dependencies and user interactions in Vitest?

Vitest mocking uses vi.fn, vi.spyOn, and controlled mocks to create deterministic tests. It isolates component behavior and validates user interactions through user-event simulations within structured describe blocks.

How do I test asynchronous React components and hooks in Vitest?

Async testing in Vitest uses findBy and waitFor assertions to handle asynchronous React component updates. It validates hooks and dynamic UI behavior reliably across components without flaky test execution.

Does this Vitest testing approach work with React Testing Library query priorities?

React Testing Library queries follow recommended accessible query priority within Vitest test suites. It applies structured querying strategies to ensure frontend tests validate actual user behavior rather than implementation details.

Can I use snapshot testing for React components with Vitest?

Vitest supports restrained snapshot testing for React components alongside hook tests. It includes snapshot usage within structured describe blocks to maintain test suite reliability while ensuring maintainable frontend validation.

When should I avoid snapshot testing in my React test suite?

Snapshot testing should be used sparingly within Vitest suites to maintain test maintainability. It prioritizes behavioral assertions and accessible queries over snapshot matching to ensure reliable component validation.