Habilidades rn-testing
🧪

rn-testing

Seguro 🌐 Acesso à rede⚙️ Comandos externos

使用 Jest 测试 React Native 应用

为 React Native 应用编写测试需要了解如何模拟原生模块、处理异步状态以及测试 Zustand 存储。本技能提供经过验证的模式和代码示例,帮助构建可靠的测试套件。

Suporta: Claude Codex Code(CC)
📊 71 Adequado
1

Baixar o ZIP da skill

2

Upload no Claude

Vá em Configurações → Capacidades → Skills → Upload skill

3

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

Seguro
v4 • 1/16/2026

This 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.

2
Arquivos analisados
631
Linhas analisadas
2
achados
4
Total de auditorias
Auditado por: claude Ver Histórico de Auditoria →

Pontuação de qualidade

38
Arquitetura
100
Manutenibilidade
85
Conteúdo
30
Comunidade
100
Segurança
91
Conformidade com especificações

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?
Zustand 存储测试
How do I test a Zustand store and prevent state from persisting between tests?
Expo 模块模拟
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() 清除模拟
  • 测试实现细节而非用户可见的行为

Perguntas Frequentes

本技能涵盖哪些测试框架?
Jest 和 React Native Testing Library 是主要框架。这些模式适用于 Expo 和非 Expo React Native 项目。
需要哪个 Jest 版本?
这些模式适用于 Jest 29+,这是使用 Expo 49 或更高版本的现代 React Native 项目中的标准版本。
如何与现有测试设置集成?
直接将模式示例复制到测试文件中。模拟配置可以添加到 jest.setup.js 以实现全局可用性。
本技能会修改我的项目文件吗?
不会。本技能基于提示提供指导。您需要手动在代码库中实现这些模式。
为什么我的测试中出现 act() 警告?
状态更新发生在 act() 包装器外部。对异步操作使用 act(),对异步内容断言使用 waitFor()。
本技能与 rn-zustand-patterns 技能有何不同?
rn-zustand-patterns 涵盖存储的实现。本技能专门关注测试使用这些存储的存储、组件和钩子。

Detalhes do Desenvolvedor

Estrutura de arquivos

📄 SKILL.md