Reputation: 1089
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
Upvotes: 0
Views: 1297
Reputation: 32080
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
Upvotes: 1
Reputation: 19
You can set env.variables inside package.json:
'parentCommand': 'cross-env CY_MODE=true npx cypress run'
Upvotes: 0