react-native-testing
react-native-testing copied to clipboard
This is how you should test your react-native components with Jest and React Native Testing Library
No More Hacks and Headaches <> Learn How to Test Your Components Before Your Users Will
Covered Examples ๐
- ๐ Clicking buttons and asserting onPress' outcome.
- ๐ฒ Filling a simple login form and asserting successful submission.
- ๐ฃ Custom hook testing (number of alternatives).
- ๐ก Mocking fetch calls.
- ๐งญ Navigating through screens with React Navigation.
- ๐ Navigating through screens with Expo Router.
- ๐ E2E feel due to real navigation throughout screens.
- ๐ฅ Handling and mocking providers.
- ๐น Mocking external lib.'s components.
- ๐ญ Mocking and interacting with RN's Modal component.
- ๐งพ Handling with a screen with RN's FlatList component.
- ๐ก Using MSW to mock api calls and handling loading/errors.
Tools in use ๐ ๏ธ
Setup and requirements ๐
Getting Started ๐
- Clone the repo
git clone [email protected]:vanGalilea/react-native-testing.git - Run
yarnto install dependencies - Explore RN CLI app and/or Expo app's tests and source code that are relevant to your use case.
RN CLI application
How to run the tests ๐โโ๏ธ
- Run
cd apps/rn-cli-appto navigate to the app folder - Run
yarn test:unitto run the unit tests - Run
yarn test:unit:devto run the unit tests in dev/watch mode - Run
yarn test:unit:coverageto run the tests and generate a coverage report
Make sure you have built and run the app in dev mode before running the e2e tests.
- Run
yarn test:e2eto run the e2e tests - Run
yarn test:e2e:devto run the e2e tests in dev/watch mode - Run
yarn test:e2e:recordto run the e2e tests and record a video of the tests
How to run the app ๐ฑ
- Run
cd apps/rn-cli-appto navigate to the app folder - Run
npx pod-installto install iOS dependencies - Run
yarn startto start the metro bundler - Click
ito run the app on iOS simulator orato run it on Android emulator
Expo application
How to run the tests ๐โโ๏ธ
- Run
cd apps/rn-expo-appto navigate to the app folder - Run
yarn test:unitto run the unit tests
How to run the app ๐ฑ
- Run
cd apps/rn-expo-appto navigate to the app folder - Run
yarn startto start the metro bundler - Click
ito run the app on iOS simulator orato run it on Android emulator
Inspiration, resources and further reading ๐
- ๐ A blog by Steve Galili on "Where and How to Start Testing Your React Native App"
- ๐ Inspired by Kent C. Dodds' workshop Test React Components with Jest and React Testing Library. For more info check Epic React.
- ๐ React Native Testing Library
- ๐งโ๐ฌ๏ธ Jest
- ๏ธโ๏ธ React Native
- ๐บ React Navigation
- ๐ฐ MSW
Impression of the project ๐ธ
https://github.com/vanGalilea/react-native-testing/assets/25864161/cdb6cdc7-7b28-4ecd-819f-52dd3c3d76c8