How to setup cypress end-to-end testing in NextJS

Setting up Cypress for end-to-end testing in a Next.js application involves a few steps. Cypress is a powerful JavaScript end-to-end testing framework that is commonly used for web applications. Below is a step-by-step guide on how to set up Cypress for end-to-end testing in a Next.js project:

Step 1: Install Cypress

First, you need to install Cypress as a development dependency in your project. Open your terminal and run:

npm install --save-dev cypress

Step 2: Configure Cypress

Create a cypress.json configuration file in your project’s root folder. This file can contain various configurations for Cypress. Here is a basic example:

{
  "baseUrl": "http://localhost:3000", // Update with your Next.js app's URL
  "fixturesFolder": "cypress/fixtures",
  "integrationFolder": "cypress/integration",
  "pluginsFile": "cypress/plugins/index.js",
  "supportFile": "cypress/support/index.js",
  "video": false
}

Step 3: Create Cypress Scripts

Open your package.json file and add the following scripts:

"scripts": {
  "test": "cypress open",
  "test:headless": "cypress run"
}

Step 4: Create Cypress Folder Structure

Create a folder named cypress in your project root. Inside this folder, create the following subfolders:

  • fixtures: For storing static data used in tests.
  • integration: For storing your test files.
  • plugins: For extending Cypress functionality (you can leave it empty for now).
  • support: For custom commands and other support files.

Step 5: Write Your First Test

Create a test file inside cypress/integration. For example, create cypress/integration/sample.spec.js with the following content:

describe('My First Test', () => {
  it('Visits the homepage', () => {
    cy.visit('/');
    cy.contains('Welcome to Next.js!'); // Update with your actual content
  });
});

Step 6: Run Cypress

Now you can run Cypress using the following commands:

npm run test

This opens the Cypress Test Runner in interactive mode. You can select the test file and see the tests running.

If you want to run tests in headless mode (for CI/CD), use:

npm run test:headless

Additional Tips:

  1. Environment Configuration:
    • Consider setting up different environment configurations in cypress.json for local, staging, and production environments.
  2. Custom Commands:
    • Explore Cypress custom commands for reusable actions across multiple tests.
  3. Assertions:
    • Use Cypress commands like cy.contains, cy.get, and various assertions to interact with and verify elements on your pages.
  4. Fixtures:
    • Store sample data in fixtures to use in your tests.
  5. Continuous Integration:
    • Integrate Cypress tests into your CI/CD pipeline.

Keep in mind that the specifics may vary based on your Next.js project structure and requirements. Always refer to the official Cypress documentation for the most up-to-date information and advanced configurations: Cypress Documentation.

Also Read:

How to change parent component value onchange of dropdown value inside child element NextJS

Build a High Level NextJS Application With B2C & B2E Login With AXIOS,JEST,CYPRESS,MUI

How to setup Jest Unit Test Framework in NextJS

How to setup the development environment and production environment variable in NextJS dynamically