install
source · Clone the upstream repo
git clone https://github.com/ComeOnOliver/skillshub
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/pproenca/dot-skills/react-testing-library" ~/.claude/skills/comeonoliver-skillshub-react-testing-library && rm -rf "$T"
manifest:
skills/pproenca/dot-skills/react-testing-library/SKILL.mdsource content
React Testing Library Best Practices
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
- Selecting queries (getByRole, getByLabelText, etc.)
- Handling async operations in tests (findBy, waitFor)
- Simulating user interactions (userEvent)
- Reviewing tests for anti-patterns and implementation detail testing
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Query Selection | CRITICAL | |
| 2 | Async Handling | CRITICAL | |
| 3 | Common Anti-Patterns | CRITICAL | |
| 4 | User Interaction | HIGH | |
| 5 | Assertions | HIGH | |
| 6 | Component Setup | MEDIUM | |
| 7 | Test Structure | MEDIUM | |
| 8 | Debugging | LOW-MEDIUM | |
| 9 | Accessibility Testing | LOW | |
Quick Reference
1. Query Selection (CRITICAL)
- Prefer getByRole over other queriesquery-prefer-role
- Avoid getByTestId as primary queryquery-avoid-testid
- Use screen for queriesquery-use-screen
- Use getByLabelText for form fieldsquery-label-text-forms
- Use name option with getByRolequery-role-name-option
- Use getBy for present, queryBy for absentquery-get-vs-query
- Use within() to scope queriesquery-within-scope
2. Async Handling (CRITICAL)
- Use findBy instead of waitFor + getByasync-findby-over-waitfor
- Always await findBy queriesasync-await-findby
- Single assertion in waitForasync-single-assertion-waitfor
- Avoid side effects in waitForasync-no-side-effects-waitfor
- Use waitForElementToBeRemovedasync-waitfor-disappear
3. Common Anti-Patterns (CRITICAL)
- Avoid unnecessary act() wrappinganti-unnecessary-act
- Remove manual cleanup callsanti-manual-cleanup
- Avoid testing implementation detailsanti-implementation-details
- Avoid empty waitFor callbacksanti-empty-waitfor
- Avoid using container for queriesanti-container-queries
- Avoid adding redundant ARIA rolesanti-redundant-roles
4. User Interaction (HIGH)
- Use userEvent over fireEventuser-prefer-userevent
- Setup userEvent before renderuser-setup-before-render
- Always await userEvent interactionsuser-await-interactions
- Use keyboard() for special keysuser-keyboard-for-special-keys
- Use clear() before retypinguser-clear-before-type
5. Assertions (HIGH)
- Use jest-dom matchersassert-jest-dom-matchers
- Use toBeVisible() for visibilityassert-visible-over-in-document
- Use toHaveTextContent() for textassert-text-content
- Use toHaveValue() for inputsassert-have-value
- Use toHaveAccessibleDescription()assert-accessible-description
6. Component Setup (MEDIUM)
- Use wrapper option for providerssetup-wrapper-providers
- Create custom render with providerssetup-custom-render
- Mock modules at module levelsetup-mock-modules
- Configure userEvent with fake timerssetup-fake-timers
- Use renderHook for testing hookssetup-render-hook
7. Test Structure (MEDIUM)
- Follow Arrange-Act-Assert patternstruct-arrange-act-assert
- Test one behavior per teststruct-one-behavior-per-test
- Use descriptive test namesstruct-descriptive-names
- Avoid render() in beforeEachstruct-avoid-beforeeach-render
8. Debugging (LOW-MEDIUM)
- Use screen.debug() to inspect DOMdebug-screen-debug
- Use logRoles to find available rolesdebug-logroles
- Use Testing Playground for queriesdebug-testing-playground
9. Accessibility Testing (LOW)
- Role queries verify accessibilitya11y-role-queries-verify
- Test focus managementa11y-verify-focus
- Test ARIA states and propertiesa11y-test-aria-states
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 |