Cypress: setting process.env to empty object

Created on 27 Aug 2019  路  9Comments  路  Source: cypress-io/cypress

Current behavior:

cy.log( "Environment: " + JSON.stringify(process.env,null,4)  );

output in browser console

Environment: {}

Desired behavior:

It should not mutate/empty the process.env.

Versions

OS: Ubuntu 18
Cypress: 3.4.1

Most helpful comment

Cypress is run in the browser environment, so there will is no access to Node in the browser nor Node's process.env within the spec files.

You can access Node, such as process.env within the support/index.js or plugins/index.js files however for use of any setup you require.

All 9 comments

Could you please provide what the process.env value is here normally?

console.log(process.env) // <-- what does this print to console?

cy.log( "Environment: " + JSON.stringify(process.env,null,4)  );

Not sure why but console.log doesn't work in case of cypress. I thought Cypress is overriding it.

@amitguptagwl it should be logging to the browser console when you run the tests.
use cypress open to open cypress in interactive mode, run the test and open browser console to see the log you added above.

Thanks @mpahuja .

@jennifer-shehane
It is {} in both case.

@mpahuja Yes, I did. My question is about why process.env is empty? Because currently, I need to make multiple tasks to pass the correct environment variables to run my tests in a different environment. On top of that, I have the tasks to run the tests for the different user journey. They again need to be duplicated per environment.

Cypress is run in the browser environment, so there will is no access to Node in the browser nor Node's process.env within the spec files.

You can access Node, such as process.env within the support/index.js or plugins/index.js files however for use of any setup you require.

Impressive that Cypress supports 5 or 6 ways of setting environment variables, but surprising that the uber-popular dotenv is not one of them.

There's a plugin for dotenv, but I've wasted the last hour trying to get it to work, to no avail.

@dandv are you still having issues getting the plugin to work?
I've tested it using Cypress 4.9 and didn't see any issues.

Was this page helpful?
0 / 5 - 0 ratings