react-testing

Test React components, hooks, context, and forms with Vitest Browser Mode and Testing Library.

Updated May 18, 2024
One-click install
npx skills add https://github.com/joshhornby/dotfiles --skill react-testing-joshhornby
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: react-testing
Source: https://github.com/joshhornby/dotfiles/tree/main/.claude/skills/react-testing
Command: npx skills add https://github.com/joshhornby/dotfiles --skill react-testing-joshhornby

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing reliable React component tests requires choosing the right harness, handling async rendering, mocking network calls, and avoiding anti-patterns like testing internal state or misusing act(). This Skill provides concrete patterns and checklists for testing React applications correctly. ## Core Features & Use Cases - Vitest Browser Mode patterns: Covers async render/renderHook with vitest-browser-react, expect.element auto-retrying assertions, locator-based interactions, portals, Suspense, and error boundaries. - Legacy Testing Library support: A deep-dive resource documents @testing-library/react with jsdom, including sync render, userEvent, findBy queries, and MSW setupServer. - Anti-pattern guidance: Flags unnecessary act() wrapping, shallow rendering, testing component internals, and shared render state, with corrected examples. - Use Case: When adding tests for a login form component, load this Skill to render the form in a real browser, fill fields via locators, mock the API with MSW setupWorker, and assert the callback payload. ## Quick Start Ask the AI to write a Vitest Browser Mode test for a React form component that fills inputs, submits, and verifies the onSubmit callback.

Frequently Asked Questions about react-testing

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

FAQPage Schema
How do I test React components with Vitest Browser Mode?

Use vitest-browser-react's async render() to mount the component, interact via locator methods like fill() and click(), and assert with expect.element() for auto-retrying checks. Configure Vitest with @vitejs/plugin-react and the Playwright browser provider.

vitest-browser-react vs @testing-library/react: which should I use?

Choose vitest-browser-react when the test claim depends on real rendering, events, focus, CSS, or browser APIs. Keep @testing-library/react with jsdom when the existing stable harness already proves pure hook, provider, or component logic at lower cost.

How do I mock API calls in Vitest Browser Mode tests?

Use MSW's setupWorker from msw/browser, not setupServer, because Browser Mode tests run in a real browser. Start the worker in a setup file and override handlers per test with worker.use().

When do I need act() in React component tests?

In vitest-browser-react, locator interactions need no act() wrapper since CDP events and retries handle timing. You only need act for hook state updates via renderHook, using the act it returns. RTL auto-wraps render and userEvent.

Can I test React Server Components in Browser Mode?

No. React Server Components execute on the server, so they cannot be tested in Browser Mode component tests. Test them with end-to-end tests via Playwright against a running app, or unit test logic extracted from the component.