Testing a React Application Setting up Cypress
• 3 min read
Introduction
The sixth part of my ongoing series is on how to test a modern React application. This time I'll go over how to initially configure Cypress, our End-to-End testing framework.
Check out the other parts here:
In the previous parts of this series, I went over how to set up our unit-testing framework with Vitest. Unit tests will cover more isolated and individual pieces of code like our functions or component-only tests if necessary.
On the other hand, Cypress and its test will handle the other big chunk where we can really gain confidence that our tests will behave similarly to how a user would use the application. Just to catch up let's go over what our simple barebones app does.
When clicking on a button it fetches posts from a service and displays them on the page:
![Testing a react application](https://cdn.hashnode.com/res/hashnode/image/upload/v1653220814601/IXI3Twik-.png align="left")
What is Cypress?
The first phrase you'll see when navigating to their page is the following:
Fast, easy and reliable testing for anything that runs in a browser.
The keyword for this quote is browser. Cypress' test runner replicates the browser environment. That means whatever test we create will behave exactly as if running in the browser. This gives us a ton of confidence!
On top of that, with Cypress we can test with several different browser environments: Google Chrome and Firefox being among them. Cypress comes with its own syntax at times but if you're familiar with Vitest or Jest then it should come naturally. The docs are actively maintained and well structured.
![thumbs up relatablecode](https://cdn.hashnode.com/res/hashnode/image/upload/v1653223455475/J4CgrxFOP.png align="left") Photo by Sincerely Media on Unsplash
Enough talking, let's start setting up Cypress.
Configuration Files for Cypress
First, we have to install the necessary dependencies:
Bash/Shellnpm install cypress --save-dev
or
Bash/Shellyarn add cypress --dev
Much like other libraries, this will have a configuration file at the root of our project: cypress.json
. Let's go ahead and create it. Within this file, you can set a variety of different flags. Check out this article in the docs to better understand all the options.
When first launching any Cypress test we have to manually visit the pages we want to test. So instead of writing out the entire URL every single time we'll go ahead and include the baseUrl
in this config.
JSON{
"baseUrl": "http://localhost:3000"
}
With that, the only other configuration we need to set up is the scripts in our package.json
.
JSON "scripts": {
// ...other scripts,
"e2e": "cypress open",
},
Check out the full repository up to this point here.
Next time we'll create our first test with Cypress as well as make sure our integration with MSW holds up.
Let's connect
If you liked this feel free to connect with me on LinkedIn or Twitter
Check out my free developer roadmap and weekly tech industry news in my newsletter.
If you see any typos or errors you can edit the article directly on GitHub
Hi, I'm Diego Ballesteros 👋.
Stay on top of the tech industry and grow as a developerwith a curated selection of articles and news alongside personal advice, observations, and insight. 🚀
No spam 🙅♂️. Unsubscribe whenever.
You may also like these articles:
- 2023-06-05T00:41:29.681Z
- 2022-10-25T11:29:26.663Z
- 2022-10-17T14:44:10.229Z
- 2022-10-10T14:02:10.349Z