AgentSkillsCN

react-testing-library-best-practices

运用React Testing Library的最佳实践,编写可维护、以用户为中心的测试用例。适用于RTL测试的编写、审核或重构工作。触发关键词包括测试文件、测试模式、getBy/queryBy查询、userEvent、waitFor以及组件测试。

SKILL.md
--- frontmatter
name: react-testing-library-best-practices
description: React Testing Library best practices for writing maintainable, user-centric tests. Use when writing, reviewing, or refactoring RTL tests. Triggers on test files, testing patterns, getBy/queryBy queries, userEvent, waitFor, and component testing.

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

PriorityCategoryImpactPrefix
1Query SelectionCRITICALquery-
2Async HandlingCRITICALasync-
3Common Anti-PatternsCRITICALanti-
4User InteractionHIGHuser-
5AssertionsHIGHassert-
6Component SetupMEDIUMsetup-
7Test StructureMEDIUMstruct-
8DebuggingLOW-MEDIUMdebug-
9Accessibility TestingLOWa11y-

Quick Reference

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)

How to Use

Read individual reference files for detailed explanations and code examples:

Reference Files

FileDescription
references/_sections.mdCategory definitions and ordering
assets/templates/_template.mdTemplate for new rules
metadata.jsonVersion and reference information