Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
Add env variable in package.json brefore cypress runs in headless mode

I want to find out if cypress runs or doesn't run in the NextJS application. My idea is prevent a some http requests in the NextJS application if the Cypress tests are running.

I am able to catch if cypress is running when I run "npx cypress open" for a browser like this:

if(window.Cypress){
 // prevent http request or do something
}

But I am not able to check if the cypress is running or not in headless mode with "npx cypress run" command.

I tried to add extra env variable in the package.json file like in below:

...
"cy:run": "cross-env CY_MODE=true cypress run",
...

But it doesn't set the CY_MODE env variable when I try to log process.env.CY_MODE.

How can I set a custom environment variable before cypress tests start with headless mode?

Thanks

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can set env.variables inside package.json:

  • create parent command where you define env.variable and contain your command for running tests 'parentCommand': 'cross-env CY_MODE=true npx cypress run'
  • launch the command and env variable will be shared between files and applications
about 4 years ago · Juan Pablo Isaza Report

0

From Exposing Environment Variables to the Browser

By default environment variables are only available in the Node.js environment, meaning they won't be exposed to the browser.

In order to expose a variable to the browser you have to prefix the variable with NEXT_PUBLIC_.

"cy:run": "cross-env NEXT_PUBLIC_CY_MODE=true cypress run",

Alternatively, you can control the NextJs server from Cypress plugins - see Mock Network When Using Next.js getServerSideProps Call

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!