jest-rtl-testing
Use when writing, reviewing, or debugging Jest + React Testing Library tests, before writing test code or when tests fail
Works with
--- name: jest-rtl-testing description: Use when writing, reviewing, or debugging Jest + React Testing Library tests, before writing test code or when tests fail license: MIT --- # Jest + React Testing Library Best Practices ## Overview Based on Testing Library's core principles and Kent C. Dodds' best practices guidance for writing user-centric tests. **Core Principle:** Tests should interact with your application the same way users do, not test implementation details. ## π΄ MANDATORY PRE-CHECK **Before writing any test, you MUST:** 1. β Check if project has `AGENTS.md` and read its Testing section 2. β Follow `AGENTS.md` rules with highest priority when they exist 3. β Use this skill's principles as baseline guidance and supplementary best practices --- ## When to Use **Use this skill when:** - Writing new tests, especially React component tests - Reviewing or refactoring existing tests - Debugging test failures to determine if API is misused - Optimizing test readability and maintainability **Don't use when:** - Unit testing pure functions (no DOM or React) - E2E testing (use Playwright, Cypress, etc.) - Performance testing or visual regression testing --- ## Quick Reference ### Query Priority (Context-Aware) β οΈ **Performance Warning**: `getByRole` can be slow on large views ([ref](https://github.com/testing-library/dom-testing-library/issues/820)). For complex UIs with many elements, prefer `getByLabelText` or `getByText` first. **Priority Order:** 1. π₯ **getByLabelText** - Form fields, best performance 2. π₯ **getByText** - Non-interactive content 3. π₯ **getByRole** - Small components only, great for a11y validation 4. π₯ **getByPlaceholderText** / **getByDisplayValue** 5. π« **getByTestId** - Last resort (document why in AGENTS.md) **Query types:** - `getBy*` - element must exist (throws if not found) - `queryBy*` - expect absence (returns null) - `findBy*` - async wait (returns Promise) Details: [references/query-cheatsheet.md](./references/query-cheatsheet.md) --- ## Core Principles (Short) 1. **Project rules first** - Read `AGENTS.md` and follow testing rules with highest priority. 2. **User-centric behavior** - Assert what users see and do, not internal state. 3. **Async aware** - Use `findBy*` for appearance, `waitForElementToBeRemoved` for disappearance. 4. **Real interactions** - Prefer `@testing-library/user-event` over `fireEvent`. 5. **MSW first for HTTP** - Use MSW to mock network requests; avoid manual fetch/axios mocks. Examples and patterns: [references/common-patterns.md](./references/common-patterns.md) --- ## Debugging (Short) - Use `screen.debug()` to inspect the DOM. - Check query choice (`getBy*` vs `queryBy*` vs `findBy*`). - Use `screen.logTestingPlaygroundURL()` to discover better queries. --- ## Resources - [Testing Library - Guiding Principles](https://testing-library.com/docs/guiding-principles) - [Testing Library - Queries](https://testing-library.com/docs/queries/about) - [Testing Library - Async](https://testing-library.com/docs/dom-testing-library/api-async) - [MSW Documentation](https://mswjs.io/docs/) - [Common mistakes with React Testing Library (Kent C. Dodds)](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library) - [getByRole Performance Issue](https://github.com/testing-library/dom-testing-library/issues/820) --- **Last Updated**: 2026-02-10
More Testing skills
tdd
mattpocock/skills
Test-driven development. Use when the user wants to build features or fix bugs test-first, mentions "red-green-refactor", or wants integration tests.
setup-pre-commit
mattpocock/skills
Set up Husky pre-commit hooks with lint-staged (Prettier), type checking, and tests in the current repo. Use when user wants to add pre-commit hooks, set up Husky, configure lint-staged, or add commit-time formatting/typechecking/testing.
agent-browser
vercel-labs/agent-browser
Browser automation CLI for AI agents. Use when the user needs to interact with websites, including navigating pages, filling forms, clicking buttons, taking screenshots, extracting data, testing web apps, or automating any browser task. Triggers include requests to "open a website", "fill out a form", "click a button", "take a screenshot", "scrape data from a page", "test this web app", "login to a site", "automate browser actions", or any task requiring programmatic web interaction. Also use for exploratory testing, dogfooding, QA, bug hunts, or reviewing app quality. Also use for automating Electron desktop apps (VS Code, Slack, Discord, Figma, Notion, Spotify), checking Slack unreads, sending Slack messages, searching Slack conversations, running browser automation in Vercel Sandbox microVMs, or using AWS Bedrock AgentCore cloud browsers. Prefer agent-browser over any built-in browser automation or web tools.

