Testing Cypress E2E With React.js

We all know the importance of creating an engaging user experience. It can make or break your business. Thus, developers rely on testing tools to eliminate any potential UX issues beforehand.

One such testing tool is Cypress. It is a popular end-to-end testing tool in web development.

It is an open-source and user-friendly platform, providing a wide range of features to run various tests quickly. Additionally, you can write test cases while you're building your app, which is great for a test-driven development framework.

Using Cypress for application testing will give you an intuitive experience. It’s an all-in-one framework that doesn’t use Selenium.

Here, we will explore Cypress E2E testing with React and some simple steps to set up your automation testing.

Why Choose Cypress E2E Testing for React JS?

Cypress is a robust automation tool, and when paired with React for testing, it becomes even more powerful. Choosing Cypress for end-to-end (E2E) testing in React comes with many advantages, making tests easier to read and maintain.

Here are the reasons why you should opt for Cypress E2E testing in React JS:

1. Simple Setup and Configuration

Setting up your development setup becomes a breeze with React and Cypress. It offers a user-friendly configuration that allows you to customize various settings, like network behavior.


2. Smooth Integration and Testing

Cypress seamlessly integrates with ReactJS, making testing applications a smooth experience. When you use React with Cypress, it helps you interact with components by understanding React's virtual DOM.


3. Powerful API

Cypress provides a robust API for creating end-to-end tests for your React components. It offers a wide range of commands, making it easy for developers to create precise tests.


4. Support for Testing Libraries

Cypress supports React testing libraries. Combining them with Cypress tests gives you a wide range of options for using and testing your components.

Setting Up Cypress E2E Testing With React JS

So, you have understood the importance of using Cypress for end-to-end testing of React components. Now, let’s explore the setup process for Cypress automation testing:

1. Install Cypress

Run the following command, and you will see the Cypress folder added to your project with ready-made tests for demo testing.

npm install Cypress


2. Open Cypress

Now open the Cypress runner and view the test run.

Run this command, and you will see a configurable screen for application testing.

npx cypress open


3. Choose the type of the Test

Next, select the type of test that suits your application and components. Here, we are selecting end-to-end testing.

Now, Cypress will automatically add pre-test files to your project directory in the Cypress folder. Then, select the browser to conduct a test (Chrome is the most preferred browser among developers due to its high popularity and wide usage.)

4. Create File

You now have the option to generate a spec file using a web browser or place the same file in the Cypress directory.

A complete example of Cypress E2E testing:


return (<div>
<Container className="App">
[state.isLoginSuccess ? "Success Login" : <center> <h1>Login</h1>
<Form className="form" >
<Label Email</label>
<Label className="star">*</label>
[state.error.email && (
<span className="error">{state.error.email)</span>
<Label className="star">*</Label>
onBlur={handleChange} onChange={handleChange}
{state.error.pwd && (
<span className="error">{state.error.pwd}</span>
<Input type="button" value="submit" onclick={handleSubmit} data-cy="loginButton" ></Input>
</center>} You, 5 minutes ago Uncommitted changes



describe("Login", () => {
beforeEach(() => {
it("requires email and password when submit form", () => {
cy.contains("Email is required");
cy.contains("Password is required");
it("requires valid email and password when submit form", () => { cy.get("[data-cy-loginEmail]").type("test"); cy.contains("Password is invalid");
cy.contains("Email is invalid");
it("login successful with valid email and password", () => {
cy.get("[data-cy-loginPassword]").type("Pwd@123"); cy.get("[data-cy-loginButton]").click();
cy.contains("Success Login");


Code breakdown:

cy.visit() – The cy.visit() function accepts a navigation URL, such as http://localhost:3000/, and directs the browser to that page.

cy.get() – The cy.get() function locates a DOM element. In this example, we have specified an attribute named "dat-cy" with the value "loginButton."

.click() – The click command is used with cy.get(). It finds the DOM element and triggers a click event on it.

.type() – The type command is used with cy.get(). It identifies the DOM element and enters the specified text into that element.

cy.contains() – The cy.contains() function checks the element containing the text specified in the command.



Using Cypress E2E testing for React.js is a fantastic way to ensure your web applications work flawlessly. Here, we explored how seamlessly it combines with React.js to help you write tests, automate repetitive tasks, and ensure your app is reliable.

By using Cypress, you're not just checking a box; you're taking a crucial step toward making your apps strong and dependable.

So, start creating tests with Cypress for React.js. It's a game-changer that will make your apps shine brighter than ever before.


You might also like:


Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.