Comprehensive testing guide for React components using Testing Library, designed for AI agents and LLMs. Contains 43 rules across 9 categories, prioritized by impact to guide test writing and code review.
When to Apply
Reference these guidelines when:
Writing new component tests with React Testing Library
struct-one-behavior-per-test - Test one behavior per test
struct-descriptive-names - Use descriptive test names
struct-avoid-beforeeach-render - Avoid render() in beforeEach
8. Debugging (LOW-MEDIUM)
debug-screen-debug - Use screen.debug() to inspect DOM
debug-logroles - Use logRoles to find available roles
debug-testing-playground - Use Testing Playground for queries
9. Accessibility Testing (LOW)
a11y-role-queries-verify - Role queries verify accessibility
a11y-verify-focus - Test focus management
a11y-test-aria-states - Test ARIA states and properties
How to Use
Read individual reference files for detailed explanations and code examples:
Section definitions - Category structure and impact levels
Rule template - Template for adding new rules
Reference Files
File
Description
references/_sections.md
Category definitions and ordering
assets/templates/_template.md
Template for new rules
metadata.json
Version and reference information
1---2name: react-testing-library3description: React Testing Library Best Practices4---5# React Testing Library Best Practices67Comprehensive testing guide for React components using Testing Library, designed for AI agents and LLMs. Contains 43 rules across 9 categories, prioritized by impact to guide test writing and code review.89## When to Apply1011Reference these guidelines when:12- Writing new component tests with React Testing Library13- Selecting queries (getByRole, getByLabelText, etc.)14- Handling async operations in tests (findBy, waitFor)15- Simulating user interactions (userEvent)16- Reviewing tests for anti-patterns and implementation detail testing1718## Rule Categories by Priority1920| Priority | Category | Impact | Prefix |21|----------|----------|--------|--------|22| 1 | Query Selection | CRITICAL | `query-` |23| 2 | Async Handling | CRITICAL | `async-` |24| 3 | Common Anti-Patterns | CRITICAL | `anti-` |25| 4 | User Interaction | HIGH | `user-` |26| 5 | Assertions | HIGH | `assert-` |27| 6 | Component Setup | MEDIUM | `setup-` |28| 7 | Test Structure | MEDIUM | `struct-` |29| 8 | Debugging | LOW-MEDIUM | `debug-` |30| 9 | Accessibility Testing | LOW | `a11y-` |3132## Quick Reference3334### 1. Query Selection (CRITICAL)3536- [`query-prefer-role`](references/query-prefer-role.md) - Prefer getByRole over other queries37- [`query-avoid-testid`](references/query-avoid-testid.md) - Avoid getByTestId as primary query38- [`query-use-screen`](references/query-use-screen.md) - Use screen for queries39- [`query-label-text-forms`](references/query-label-text-forms.md) - Use getByLabelText for form fields40- [`query-role-name-option`](references/query-role-name-option.md) - Use name option with getByRole41- [`query-get-vs-query`](references/query-get-vs-query.md) - Use getBy for present, queryBy for absent42- [`query-within-scope`](references/query-within-scope.md) - Use within() to scope queries4344### 2. Async Handling (CRITICAL)4546- [`async-findby-over-waitfor`](references/async-findby-over-waitfor.md) - Use findBy instead of waitFor + getBy47- [`async-await-findby`](references/async-await-findby.md) - Always await findBy queries48- [`async-single-assertion-waitfor`](references/async-single-assertion-waitfor.md) - Single assertion in waitFor49- [`async-no-side-effects-waitfor`](references/async-no-side-effects-waitfor.md) - Avoid side effects in waitFor50- [`async-waitfor-disappear`](references/async-waitfor-disappear.md) - Use waitForElementToBeRemoved5152### 3. Common Anti-Patterns (CRITICAL)5354- [`anti-unnecessary-act`](references/anti-unnecessary-act.md) - Avoid unnecessary act() wrapping55- [`anti-manual-cleanup`](references/anti-manual-cleanup.md) - Remove manual cleanup calls56- [`anti-implementation-details`](references/anti-implementation-details.md) - Avoid testing implementation details57- [`anti-empty-waitfor`](references/anti-empty-waitfor.md) - Avoid empty waitFor callbacks58- [`anti-container-queries`](references/anti-container-queries.md) - Avoid using container for queries59- [`anti-redundant-roles`](references/anti-redundant-roles.md) - Avoid adding redundant ARIA roles6061### 4. User Interaction (HIGH)6263- [`user-prefer-userevent`](references/user-prefer-userevent.md) - Use userEvent over fireEvent64- [`user-setup-before-render`](references/user-setup-before-render.md) - Setup userEvent before render65- [`user-await-interactions`](references/user-await-interactions.md) - Always await userEvent interactions66- [`user-keyboard-for-special-keys`](references/user-keyboard-for-special-keys.md) - Use keyboard() for special keys67- [`user-clear-before-type`](references/user-clear-before-type.md) - Use clear() before retyping6869### 5. Assertions (HIGH)7071- [`assert-jest-dom-matchers`](references/assert-jest-dom-matchers.md) - Use jest-dom matchers72- [`assert-visible-over-in-document`](references/assert-visible-over-in-document.md) - Use toBeVisible() for visibility73- [`assert-text-content`](references/assert-text-content.md) - Use toHaveTextContent() for text74- [`assert-have-value`](references/assert-have-value.md) - Use toHaveValue() for inputs75- [`assert-accessible-description`](references/assert-accessible-description.md) - Use toHaveAccessibleDescription()7677### 6. Component Setup (MEDIUM)7879- [`setup-wrapper-providers`](references/setup-wrapper-providers.md) - Use wrapper option for providers80- [`setup-custom-render`](references/setup-custom-render.md) - Create custom render with providers81- [`setup-mock-modules`](references/setup-mock-modules.md) - Mock modules at module level82- [`setup-fake-timers`](references/setup-fake-timers.md) - Configure userEvent with fake timers83- [`setup-render-hook`](references/setup-render-hook.md) - Use renderHook for testing hooks8485### 7. Test Structure (MEDIUM)8687- [`struct-arrange-act-assert`](references/struct-arrange-act-assert.md) - Follow Arrange-Act-Assert pattern88- [`struct-one-behavior-per-test`](references/struct-one-behavior-per-test.md) - Test one behavior per test89- [`struct-descriptive-names`](references/struct-descriptive-names.md) - Use descriptive test names90- [`struct-avoid-beforeeach-render`](references/struct-avoid-beforeeach-render.md) - Avoid render() in beforeEach9192### 8. Debugging (LOW-MEDIUM)9394- [`debug-screen-debug`](references/debug-screen-debug.md) - Use screen.debug() to inspect DOM95- [`debug-logroles`](references/debug-logroles.md) - Use logRoles to find available roles96- [`debug-testing-playground`](references/debug-testing-playground.md) - Use Testing Playground for queries9798### 9. Accessibility Testing (LOW)99100- [`a11y-role-queries-verify`](references/a11y-role-queries-verify.md) - Role queries verify accessibility101- [`a11y-verify-focus`](references/a11y-verify-focus.md) - Test focus management102- [`a11y-test-aria-states`](references/a11y-test-aria-states.md) - Test ARIA states and properties103104## How to Use105106Read individual reference files for detailed explanations and code examples:107108- [Section definitions](references/_sections.md) - Category structure and impact levels109- [Rule template](assets/templates/_template.md) - Template for adding new rules110111## Reference Files112113| File | Description |114|------|-------------|115| [references/_sections.md](references/_sections.md) | Category definitions and ordering |116| [assets/templates/_template.md](assets/templates/_template.md) | Template for new rules |117| [metadata.json](metadata.json) | Version and reference information |
Run npx skillmds@latest add comeonoliver/react-testing-library in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
React Testing Library Best Practices It is listed under Web & Frontend on SkillMD.
This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
ComeOnOliver (@comeonoliver) published this skill. Their other Agent Skills are listed on their SkillMD profile.