CalvinTorra

Cypress + Next.js Environment Variables


Signing in to an app using Cypress, but don’t want to hard code the values to accidently upload them to github?

Using dotenv also isn’t a simple set up here, so here’s the workaround.

  • Create a cypress.env.json file at the root of the project.
  • Add it to the gitignore file.
  • Create your key value pairs and index into it using Cypress.env(‘ObjectName’).key
{
  "userOne": {
    "email": "emailAddy@sliame.com",
    "password": "thePassWordWord"
  }
}
let username = Cypress.env("userOne").email;

cy.visit("/");
cy.get('[data-test="sign-in-email"]').type(username);