Testing Framework
React Native uses Jest as its default testing framework. Jest is pre-configured in React Native projects.Jest Configuration
React Native’s Jest configuration is located injest.config.js:
Unit Testing Components
React Native Testing Library
Install React Native Testing Library:Setup
Createjest.setup.js:
Basic Component Test
Testing Async Operations
Mocking Native Modules
Manual Mocks
Create__mocks__ directory:
Mock React Native Modules
Mock AsyncStorage
Testing Navigation
React Navigation Mocking
Testing Hooks
renderHook Utility
Testing Custom Hooks with Dependencies
Snapshot Testing
Integration Testing
Testing User Flows
End-to-End Testing
Detox
Detox is a popular E2E testing framework for React Native.Installation
Configuration
Add topackage.json:
E2E Test Example
Code Coverage
Running with Coverage
Coverage Configuration
Add tojest.config.js:
Best Practices
- Test behavior, not implementation: Focus on what the component does, not how it does it
- Use data-testid sparingly: Prefer accessible queries like
getByTextorgetByRole - Mock external dependencies: Keep tests isolated and fast
- Write integration tests: Test how components work together
- Keep tests maintainable: Avoid brittle selectors and over-mocking
- Test error states: Don’t just test the happy path
- Use TypeScript: Get type safety in your tests
CI/CD Integration
GitHub Actions Example
Next Steps
Fast Refresh
Enable instant feedback during development
Troubleshooting
Solve common testing issues