codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚛️

React.js

46 / 61 topics
43Introduction to React Testing44Unit Testing with Jest45Snapshot Testing in React46End-to-End Testing with Cypress47Using React Developer Tools for Debugging
Tutorials/React.js/End-to-End Testing with Cypress
⚛️React.js

End-to-End Testing with Cypress

Updated 2026-04-20
3 min read

Introduction

In this section, we will explore how to perform end-to-end (E2E) testing for a React.js application using Cypress. E2E testing is crucial for ensuring that all parts of your application work together as expected from the user's perspective. Cypress is a powerful and intuitive tool that simplifies the process of writing and running E2E tests.

Prerequisites

Before we dive into the tutorial, ensure you have the following:

  • Node.js installed on your machine.
  • A React.js project set up. If not, you can create one using Vite:
    npm create vite@latest my-app --template react
    cd my-app
    

Setting Up Cypress

  1. Install Cypress: First, we need to install Cypress as a development dependency in our project.

    npm install cypress --save-dev
    
  2. Open Cypress: After installation, you can open Cypress by running:

    npx cypress open
    

    This command will launch the Cypress Test Runner, where you can create and run your tests.

  3. Project Structure: Cypress creates a cypress folder in your project root with the following structure:

    • integration: This is where your test files will reside.
    • fixtures: Place any static data files here that your tests might need.
    • support: Contains utility functions and hooks for Cypress commands.

Writing Your First Test

Let's write a simple E2E test to verify that our React app renders correctly. Assume we have a basic React component called App in src/App.js.

Step 1: Create a Test File

Create a new file named app.spec.js inside the cypress/integration directory.

// cypress/integration/app.spec.js

describe('My App', () => {
  it('should render the app title', () => {
    // Visit the root URL of your application
    cy.visit('/');

    // Check if the app title is visible
    cy.contains('h1', 'Welcome to My App').should('be.visible');
  });
});

Step 2: Run the Test

In the Cypress Test Runner, you should see your test listed under My App. Click on it to run the test. Cypress will open a browser window and execute the steps defined in your test.

Understanding Cypress Commands

Cypress provides a rich set of commands to interact with web pages. Here are some commonly used commands:

  • cy.visit(url): Navigates to the specified URL.
  • cy.get(selector): Selects an element using a CSS selector.
  • cy.contains(content): Finds an element containing specific text content.
  • cy.type(text): Types into a form field.
  • cy.click(): Clicks on an element.

Example: Testing Form Submission

Let's add another test to verify that a form submission works correctly. Assume we have a simple form in src/App.js.

// cypress/integration/app.spec.js

describe('My App', () => {
  it('should render the app title', () => {
    cy.visit('/');
    cy.contains('h1', 'Welcome to My App').should('be.visible');
  });

  it('should submit a form and display a success message', () => {
    cy.visit('/');

    // Type into the input field
    cy.get('input[name="name"]').type('John Doe');

    // Submit the form
    cy.get('button[type="submit"]').click();

    // Check if the success message is displayed
    cy.contains('.success-message', 'Form submitted successfully!').should('be.visible');
  });
});

Best Practices

  1. Use Descriptive Test Names: Ensure your test names are clear and describe what they are testing.
  2. Avoid Hardcoding URLs: Use environment variables or configuration files to manage URLs for different environments (development, staging, production).
  3. Handle Asynchronous Operations: Cypress commands are asynchronous by default, so use .then() or await when necessary.
  4. Use Fixtures for Static Data: Store static data in JSON files under the cypress/fixtures directory and load them using cy.fixture().
  5. Write Tests for Edge Cases: Don't just test happy paths; also consider edge cases and error handling.

Advanced Features

Custom Commands

You can create custom Cypress commands to encapsulate repetitive actions. For example, let's create a command to log in a user:

// cypress/support/commands.js

Cypress.Commands.add('login', (username, password) => {
  cy.visit('/login');
  cy.get('input[name="username"]').type(username);
  cy.get('input[name="password"]').type(password);
  cy.get('button[type="submit"]').click();
});

Now you can use this custom command in your tests:

// cypress/integration/app.spec.js

describe('My App', () => {
  it('should log in a user and redirect to dashboard', () => {
    cy.login('user@example.com', 'password123');
    cy.url().should('include', '/dashboard');
  });
});

Environment Variables

Cypress supports environment variables, which can be used to manage different configurations for various environments. You can define these in a cypress.env.json file or pass them as command-line arguments.

// cypress.env.json

{
  "baseUrl": "http://localhost:3000"
}

Then, you can access the environment variable in your tests:

// cypress/integration/app.spec.js

describe('My App', () => {
  it('should render the app title', () => {
    cy.visit(Cypress.env('baseUrl'));
    cy.contains('h1', 'Welcome to My App').should('be.visible');
  });
});

Conclusion

In this tutorial, we learned how to set up and use Cypress for E2E testing in a React.js application. We covered the basics of writing tests, understanding Cypress commands, and implementing best practices. Additionally, we explored advanced features like custom commands and environment variables.

By following these guidelines, you can ensure that your React.js application is robust and free from bugs through comprehensive end-to-end testing with Cypress.


PreviousSnapshot Testing in ReactNext Using React Developer Tools for Debugging

Recommended Gear

Snapshot Testing in ReactUsing React Developer Tools for Debugging