Testing Library
前端开发 IT 技术知识导航
知识导航面板
知识速览
为什么测试要像用户一样思考
核心机制:查询优先级与异步等待
选型决策:什么时候用 Testing Library
学习路线
第一个测试
- render / screen / getByRole 基础用法
- 安装与 @testing-library/react 配置
- 与 Jest / Vitest 测试运行器配合
异步测试
- findBy 查询(Promise 化的 getBy)
- waitFor 等待异步状态更新
- MSW(Mock Service Worker)模拟 API 请求
查询与交互
- 查询变体(getBy / queryBy / findBy)语义区别
- 无障碍查询(ByRole / ByLabelText / ByPlaceholderText)
- userEvent 模拟用户交互(click / type / keyboard)
高级模式
- 自定义 render(包裹 Provider / Router / Theme)
- 列表与虚拟滚动测试
- 表单测试(React Hook Form / Formik)
测试架构
- 测试文件组织(co-located vs __tests__ 目录)
- 自定义测试工具函数(test-utils.tsx)
常见陷阱
- 避免测试实现细节(不要查 className / state)
- act() warning 排查与修复
资源库
同类替代
- Playwright E2E 测试
- Cypress E2E 测试
延伸资源
- Common mistakes with Testing Library
官方资源
- Testing Library 查询优先级指南
- Testing Library