Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here are the steps to designate the .env file to be utilized for the dev server while carrying out Cypress tests:

  1. Create a .env file in the root directory of your project.
  2. Add your environment variables to the .env file. For example:
API_URL=https://api.example.com
  1. Install the cypress-env plugin by running the following command:
npm install --save-dev cypress-env
  1. In your Cypress configuration file, add the following code:
const cypressEnv = require('cypress-env')

module.exports = (on, config) => {
  return cypressEnv.config(config)
}

This code will load the environment variables from your .env file into the Cypress configuration.

  1. Start your development server with the following command:
API_URL=http://localhost:3000 cypress open

This command sets the API_URL environment variable to the development server's URL.

  1. In your Cypress test files, access the environment variables with the Cypress.env() method. For example:
cy.visit(Cypress.env('API_URL'))