unit-testing

Write unit tests for React TypeScript components, hooks, and utilities with Vitest, React Testing Library, and MSW.

3|Updated Apr 5, 2024
One-click install
npx skills add https://github.com/danghungtb26/admin-react-typescript-template --skill unit-testing-danghungtb26
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: unit-testing
Source: https://github.com/danghungtb26/admin-react-typescript-template/tree/main/.agents/skills/unit-testing
Command: npx skills add https://github.com/danghungtb26/admin-react-typescript-template --skill unit-testing-danghungtb26

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Unit testing React TypeScript projects can be time-consuming and brittle without a clear pattern. This Skill provides a structured guide for creating reliable, maintainable tests using Vitest, React Testing Library, and MSW, covering components, hooks, and utilities.

Core Features & Use Cases

  • AAA (Arrange-Act-Assert) testing pattern guidance for consistent test structure
  • Mocking strategies and MSW setup to simulate API interactions
  • Test organization, coverage guidelines, and practical examples across components, hooks, and utilities
  • Real-world workflows: writing tests, refactoring with confidence, and maintaining test suites

Quick Start

  • Run all tests: pnpm test
  • Watch mode: pnpm test:watch
  • Generate a coverage report: pnpm test:coverage

Frequently Asked Questions about unit-testing

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

FAQPage Schema
How do I write unit tests for React TypeScript components using Vitest?

You structure unit tests for React TypeScript components using the AAA pattern with Vitest and React Testing Library, rendering components and asserting behavior to produce maintainable test suites that validate UI interactions.

What is the AAA pattern in unit testing and when should I use it?

The AAA pattern organizes unit tests into Arrange, Act, and Assert phases, ensuring consistent test structure across components, hooks, and utilities to create reliable and maintainable test suites.

How do I mock API requests in React tests with MSW?

Mocking API requests in React tests with MSW involves setting up request handlers to simulate API interactions, allowing you to isolate components from network dependencies and write predictable, deterministic unit tests.

How do I generate a code coverage report for Vitest in a React project?

Generating a code coverage report for Vitest involves running the coverage command, which analyzes test suite execution to output a detailed report highlighting tested and untested code across your React TypeScript project.

Does this unit testing guide work for testing custom React hooks?

Yes, this unit testing guide works for custom React hooks by providing specific examples and strategies for testing hooks using Vitest and React Testing Library to ensure correct state management and side effects.