--- name: react-testing description: Testing patterns for Vitest, React Testing Library, and Jest. Routes to component, hook, and integration test examples. allowed-tools: Read Write Edit Grep Glob Bash --- # React Testing ## Overview This skill provides comprehensive guidance for testing React applications using Vitest, React Testing Library, and Jest. Apply these patterns when writing unit tests, integration tests, and ensuring code quality. ## Core Philosophy - **Test behavior, not implementation**: Focus on what users see and do - **Avoid testing internal state**: Test public APIs and user interactions - **Write tests that give confidence**: Catch real bugs, not false positives - **Keep tests simple**: Tests should be easier to understand than the code ## Testing Tools ### Vitest Configuration ```ts // vitest.config.ts import { defineConfig } from 'vitest/config'; import react from '@vitejs/plugin-react'; export default defineConfig({ plugins: [react()], test: { globals: true, environment: 'jsdom', setupFiles: './src/test/setup.ts', coverage: { provider: 'v8', reporter: ['text', 'json', 'html'], exclude: ['node_modules/', 'src/test/'] } } }); ``` ### Test Setup ```ts // src/test/setup.ts import { expect, afterEach } from 'vitest'; import { cleanup } from '@testing-library/react'; import * as matchers from '@testing-library/jest-dom/matchers'; expect.extend(matchers); afterEach(() => { cleanup(); }); ``` ## Component Testing ### Basic Component Test ```tsx import { render, screen } from '@testing-library/react'; import { expect, test } from 'vitest'; import { Button } from './Button'; test('renders button with label', () => { render(