rn-testing
使用 Jest 测试 React Native 应用
为 React Native 应用编写测试需要了解如何模拟原生模块、处理异步状态以及测试 Zustand 存储。本技能提供经过验证的模式和代码示例,帮助构建可靠的测试套件。
Baixar o ZIP da skill
Upload no Claude
Vá em Configurações → Capacidades → Skills → Upload skill
Ative e comece a usar
Testar
A utilizar "rn-testing". How do I test a Zustand store?
Resultado esperado:
- Reset the store before each test using setState(initialState, true) to replace (not merge) state
- Use getState() to access fresh state after async operations
- Wrap async store actions in act() when testing
- Example: beforeEach(() => { useAssessmentStore.setState(initialState, true); });
A utilizar "rn-testing". How do I mock expo-router in tests?
Resultado esperado:
- Use jest.mock('expo-router', () => ({ useRouter: () => ({ push: jest.fn(), replace: jest.fn(), back: jest.fn() }) }))
- Configure mocks globally in jest.setup.js for consistent behavior across all test files
- Reset mocks between tests with jest.clearAllMocks() in beforeEach
- Mock useLocalSearchParams to return test data for screens that read route params
A utilizar "rn-testing". How do I fix act() warnings in my tests?
Resultado esperado:
- Wrap all state updates and async operations in act() to ensure proper timing
- Use waitFor() for assertions on async content that may not appear immediately
- Prefer findBy* queries over getBy* for async content (findBy has built-in waiting)
- Check for hanging promises that resolve after the test completes
Auditoria de Segurança
SeguroThis skill is pure documentation containing only testing patterns for React Native. The static scanner incorrectly flagged markdown code blocks and JavaScript method names. No executable code, network calls, or file operations exist in this skill.
Fatores de risco
🌐 Acesso à rede (2)
⚙️ Comandos externos (27)
Pontuação de qualidade
O Que Você Pode Construir
修复不稳定的存储测试
了解如何在测试之间重置 Zustand 存储状态,防止状态泄漏导致间歇性测试失败。
模拟 Expo 原生模块
在 Jest 测试中为 expo-router、expo-secure-store 和其他 Expo 模块设置适当的模拟。
测试异步操作
使用 act() 和 waitFor 处理测试中的异步存储操作和 React Query 数据获取。
Tente Estes Prompts
How do I test a React Native component with Jest and React Native Testing Library?
How do I test a Zustand store and prevent state from persisting between tests?
Show me how to mock expo-router and expo-secure-store in Jest tests.
My React Native tests have act() warnings and timeouts. How do I fix them?
Melhores Práticas
- 始终在 beforeEach 中重置存储状态,以防止测试之间的测试污染
- 对异步内容使用 findBy* 查询而非 getBy*,以避免竞态条件
- 在模块级别模拟外部依赖(如 expo-router)以获得一致的行为
Evitar
- 对可能不会立即出现的异步内容使用 getBy* 查询
- 在导航测试之间忘记使用 jest.clearAllMocks() 清除模拟
- 测试实现细节而非用户可见的行为