React Native Web Component Testing Using Cypress And Vite
Bundle React Native app using Vite to run on the web and write Cypress component tests.
If you can bundle your web projects using Vite, then you can write Cypress component tests against it. For example, I have shown how to test components from ArrowJS framework when bundled using Vite. In this blog post, I will describe a ReactNative project running inside the browser via react-native-web. We will use Vite to bundle the project, thus we will be able to write Cypress component tests!
Note: you might ask me why I prefer those clunky attribute selectors like [role=heading] and [role=button][data-testid=ToInfo]. I love these standard CSS selectors because I can check them or use them right from the browser's DevTools elements panel.
Component testing
I have added Component testing to Cypress and picked "React" with "Vite". After all - our project has both 😉
it('navigates screen to screen', () => { cy.mount( <NavigationContainer> <Navigator> <Screenname={'Home'} component={Home} /> <Screenname={'Info'} component={Info} /> </Navigator> </NavigationContainer>, ) cy.contains('[role=button]', 'Navigate to Info').click() cy.contains('[role=button]', 'Navigate to Home').click() cy.contains('[role=button]', 'Navigate to Info').should('be.visible') })
The navigation test goes from one screen to another and back. Looks a lot like an end-to-end test, doesn't it? Yup - once mounted, the React Native (web) component runs like a mini web application. No shallow rendering - all children components should work and be tested by the test runner interactions.
Continuous integration
On every commit, we will run all tests (end-to-end and component) using GitHub Actions. Here is my workflow file using bahmutov/cypress-workflows reusable workflows. For the latest workflow files, see .github/workflows.
.github/workflows/ci.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
name:ci on: [push] jobs: e2e: # use the reusable workflow to check out the code, install dependencies # and run the Cypress tests # https://github.com/bahmutov/cypress-workflows uses:bahmutov/cypress-workflows/.github/workflows/standard.yml@v1 with: start:npmrundev