hieutrtr/ai1-skills/react-testing-patterns
Claude Code and agent skill by hieutrtr/ai1-skills
- community
- ext:[{"type":"hook","description":"Pre/post lifecycle hooks"},{"type":"context","description":"Custom context provider definitions"}]
Checking that the SKILL.md file is still in the repository.
React component and hook testing patterns with Testing Library and Vitest. Use when writing tests for React components, custom hooks, or data fetching logic. Covers component rendering tests, user interaction simulation, async state testing, MSW for API mocking, hook testing with renderHook, accessibility assertions, and snapshot testing guidelines. Does NOT cover E2E tests (use e2e-testing) or TDD workflow enforcement (use tdd-workflow).
Install
npx vskill@latest install hieutrtr/ai1-skills/react-testing-patternsSemi-extensibleMentions customization, but needs skill-specific knowledge. Read the extensible skills standard
SKILL.md
React Testing Patterns
When to Use
Activate this skill when:
- Writing tests for React components (rendering, interaction, accessibility)
- Testing custom hooks with
renderHook - Mocking API calls with MSW (Mock Service Worker)
- Testing async state changes (loading, error, success)
- Auditing component accessibility with jest-axe
- Setting up test infrastructure (providers, test utilities)
Do NOT use this skill for:
- E2E browser tests with Playwright (use
e2e-testing) - Backend Python tests (use
pytest-patterns) - TDD workflow enforcement (use
tdd-workflow) - Writing component implementation code (use
react-frontend-expert)
Instructions
Testing Library Philosophy
Core principle: Test behavior, not implementation.
Query priority (prefer higher in the list):
getByRole— accessible role (button, heading, textbox)getByLabelText— form elements with labelsgetByPlaceholderText— input placeholdersgetByText— visible text contentgetByDisplayValue— current form input valuegetByAltText— imagesgetByTestId— last resort (data-testid attribute)
Interaction: Always use userEvent over fireEvent:
What NOT to test:
- Internal component state (don't test
useStatevalues directly) - CSS classes or styles
- Component instance methods
- Which hooks were called
- Snapshot tests for dynamic content
- Third-party library internals
Component Test Structure
Every component test follows Arrange → Act → Assert:
Async Testing
waitFor (wait for state update)
findBy (built-in waitFor)
Prefer `findBy*` over `waitFor` + `getBy*` for elements that appear asynchronously.
Testing Error States
MSW API Mocking
Setup a mock server for all API tests:
Per-test handler override:
Hook Testing
Testing hooks with TanStack Query:
Accessibility Testing
Add to every component test file:
Test Utility: Custom Render
Create a custom render that wraps components with required providers:
Examples
Testing a Form Component
Edge Cases
- Components with providers: Always use a custom render function that wraps components with
QueryClientProvider,MemoryRouter, and any context providers needed.
- Components with router: Use
<MemoryRouter initialEntries={["/users/1"]}>for components that useuseParamsoruseNavigate.
- Flaky async tests: Prefer
findBy*overwaitFor+getBy*. If usingwaitFor, increase timeout for CI:waitFor(() => ..., { timeout: 5000 }).
- Testing modals/portals: Use
screenqueries (they search the entire document), notcontainerqueries.
- Cleanup: Testing Library auto-cleans after each test. Don't call
cleanup()manually unless using a custom setup.
See references/component-test-template.tsx for an annotated test file template. See references/msw-handler-examples.ts for MSW handler patterns. See references/hook-test-template.tsx for hook testing patterns.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/react-testing-patterns/SKILL.md
Popularity
- Repository stars
- 8
- Repository forks
- 2
- 7-day trend score
- 11
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 16 Mar 2026, 8 months ago.
Extension points
hookPre/post lifecycle hookscontextCustom context provider definitions
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/hieutrtr/ai1-skills/react-testing-patterns)