react-testing-library
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseReact Testing Library Best Practices
React Testing Library 最佳实践
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.
这是一份针对使用Testing Library测试React组件的综合指南,专为AI Agent和LLM设计。包含9个类别共43条规则,按影响优先级排序,用于指导测试编写和代码评审。
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
在以下场景中参考这些指南:
- 使用React Testing Library编写新的组件测试时
- 选择查询方法(getByRole、getByLabelText等)时
- 处理测试中的异步操作(findBy、waitFor)时
- 模拟用户交互(userEvent)时
- 评审测试以发现反模式和对实现细节的测试时
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 | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 查询选择 | 关键 | |
| 2 | 异步处理 | 关键 | |
| 3 | 常见反模式 | 关键 | |
| 4 | 用户交互 | 高 | |
| 5 | 断言 | 高 | |
| 6 | 组件设置 | 中 | |
| 7 | 测试结构 | 中 | |
| 8 | 调试 | 低-中 | |
| 9 | 可访问性测试 | 低 | |
Quick Reference
快速参考
1. Query Selection (CRITICAL)
1. 查询选择(关键)
- - Prefer getByRole over other queries
query-prefer-role - - Avoid getByTestId as primary query
query-avoid-testid - - Use screen for queries
query-use-screen - - Use getByLabelText for form fields
query-label-text-forms - - Use name option with getByRole
query-role-name-option - - Use getBy for present, queryBy for absent
query-get-vs-query - - Use within() to scope queries
query-within-scope
- - 优先使用getByRole而非其他查询方法
query-prefer-role - - 避免将getByTestId作为主要查询方法
query-avoid-testid - - 使用screen进行查询
query-use-screen - - 对表单字段使用getByLabelText
query-label-text-forms - - 结合name选项使用getByRole
query-role-name-option - - 存在元素时用getBy,不存在时用queryBy
query-get-vs-query - - 使用within()限定查询范围
query-within-scope
2. Async Handling (CRITICAL)
2. 异步处理(关键)
- - Use findBy instead of waitFor + getBy
async-findby-over-waitfor - - Always await findBy queries
async-await-findby - - Single assertion in waitFor
async-single-assertion-waitfor - - Avoid side effects in waitFor
async-no-side-effects-waitfor - - Use waitForElementToBeRemoved
async-waitfor-disappear
- - 使用findBy而非waitFor + getBy
async-findby-over-waitfor - - 始终对findBy查询使用await
async-await-findby - - 在waitFor中仅包含单个断言
async-single-assertion-waitfor - - 避免在waitFor中产生副作用
async-no-side-effects-waitfor - - 使用waitForElementToBeRemoved
async-waitfor-disappear
3. Common Anti-Patterns (CRITICAL)
3. 常见反模式(关键)
- - Avoid unnecessary act() wrapping
anti-unnecessary-act - - Remove manual cleanup calls
anti-manual-cleanup - - Avoid testing implementation details
anti-implementation-details - - Avoid empty waitFor callbacks
anti-empty-waitfor - - Avoid using container for queries
anti-container-queries - - Avoid adding redundant ARIA roles
anti-redundant-roles
- - 避免不必要的act()包裹
anti-unnecessary-act - - 移除手动清理调用
anti-manual-cleanup - - 避免测试实现细节
anti-implementation-details - - 避免空的waitFor回调
anti-empty-waitfor - - 避免使用container进行查询
anti-container-queries - - 避免添加冗余的ARIA角色
anti-redundant-roles
4. User Interaction (HIGH)
4. 用户交互(高)
- - Use userEvent over fireEvent
user-prefer-userevent - - Setup userEvent before render
user-setup-before-render - - Always await userEvent interactions
user-await-interactions - - Use keyboard() for special keys
user-keyboard-for-special-keys - - Use clear() before retyping
user-clear-before-type
- - 使用userEvent而非fireEvent
user-prefer-userevent - - 在渲染前设置userEvent
user-setup-before-render - - 始终对userEvent交互使用await
user-await-interactions - - 对特殊按键使用keyboard()
user-keyboard-for-special-keys - - 重新输入前使用clear()
user-clear-before-type
5. Assertions (HIGH)
5. 断言(高)
- - Use jest-dom matchers
assert-jest-dom-matchers - - Use toBeVisible() for visibility
assert-visible-over-in-document - - Use toHaveTextContent() for text
assert-text-content - - Use toHaveValue() for inputs
assert-have-value - - Use toHaveAccessibleDescription()
assert-accessible-description
- - 使用jest-dom匹配器
assert-jest-dom-matchers - - 使用toBeVisible()判断可见性
assert-visible-over-in-document - - 使用toHaveTextContent()判断文本
assert-text-content - - 使用toHaveValue()判断输入框值
assert-have-value - - 使用toHaveAccessibleDescription()
assert-accessible-description
6. Component Setup (MEDIUM)
6. 组件设置(中)
- - Use wrapper option for providers
setup-wrapper-providers - - Create custom render with providers
setup-custom-render - - Mock modules at module level
setup-mock-modules - - Configure userEvent with fake timers
setup-fake-timers - - Use renderHook for testing hooks
setup-render-hook
- - 使用wrapper选项配置提供者
setup-wrapper-providers - - 创建带有提供者的自定义render
setup-custom-render - - 在模块级别模拟模块
setup-mock-modules - - 结合fake timers配置userEvent
setup-fake-timers - - 使用renderHook测试钩子
setup-render-hook
7. Test Structure (MEDIUM)
7. 测试结构(中)
- - Follow Arrange-Act-Assert pattern
struct-arrange-act-assert - - Test one behavior per test
struct-one-behavior-per-test - - Use descriptive test names
struct-descriptive-names - - Avoid render() in beforeEach
struct-avoid-beforeeach-render
- - 遵循Arrange-Act-Assert模式
struct-arrange-act-assert - - 每个测试仅验证一个行为
struct-one-behavior-per-test - - 使用描述性的测试名称
struct-descriptive-names - - 避免在beforeEach中调用render()
struct-avoid-beforeeach-render
8. Debugging (LOW-MEDIUM)
8. 调试(低-中)
- - Use screen.debug() to inspect DOM
debug-screen-debug - - Use logRoles to find available roles
debug-logroles - - Use Testing Playground for queries
debug-testing-playground
- - 使用screen.debug()检查DOM
debug-screen-debug - - 使用logRoles查找可用角色
debug-logroles - - 使用Testing Playground编写查询
debug-testing-playground
9. Accessibility Testing (LOW)
9. 可访问性测试(低)
- - Role queries verify accessibility
a11y-role-queries-verify - - Test focus management
a11y-verify-focus - - Test ARIA states and properties
a11y-test-aria-states
- - 通过角色查询验证可访问性
a11y-role-queries-verify - - 测试焦点管理
a11y-verify-focus - - 测试ARIA状态和属性
a11y-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 |
| 文件 | 描述 |
|---|---|
| references/_sections.md | 类别定义和排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本和参考信息 |