Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding configuration variables from cypress.env.json

TL;DR:

I am trying to override the baseUrl value from cypress.json using my cypress.env.json file, but I can't seem to figure out how. Is there a way to do this?

Background

Setting environment variables in the cypress.json file and later overriding them in cypress.env.json is as easy as pie. In cypress.json:

{
  "env": {
    "someVariable": "originalValue"
  }
}

... and in cypress.env.json:

{
  "someVariable": "newValue"
}

Regarding configuration variables, the documentation states:

If your environment variables match a standard configuration key, then instead of setting an environment variable they will instead override the configuration value.

However, if I try setting baseUrl from cypress.json...

{
  "baseUrl": "http://example.com/setFromCypress.json",
  "env": {
    "someVariable": "originalValue"
  }
}

... and overriding it in cypress.env.json ...

{
  "baseUrl": "http://example.com/setFromCypress.env.json",
  "someVariable": "newValue"
}

... then someVariable is overriden, but the existing baseUrl remains unchanged (and a baseUrl appears inside the object placed at the env key):

A picture of the Cypress configuration when setting baseUrl from both cypress.json and cypress.env.json

I have no problem when setting baseUrl in cypress.json and later overriding it in the command line using CYPRESS_BASE_URL:

$ export CYPRESS_BASE_URL=http://example.com/setFromCommandLine

Then, the original baseUrl is overriden:

A picture of the Cypress configuration when setting baseUrl from cypress.json, cypress.env.json and the command line

To summarize: Am I missing something in the documentation, or is something missing in the documentation?

like image 759
vages Avatar asked Nov 13 '17 10:11

vages


People also ask

How do I override Cypress environment variables?

You can pass in environment variables as options when using the CLI tool. Values here will overwrite all other conflicting environment variables. You can use the --env argument for cypress run. Multiple values must be separated by a comma, not a space.

How do you override an environment variable?

Windows 8 and Windows 10In the User variables section, click New to open the New User Variable dialog box. Enter the name of the variable and its value, and click OK. The variable is added to the User variables section of the Environment Variables dialog box. Click OK in the Environment Variables dialog box.

How do I change config in Cypress?

If you want to apply different settings, you need to write a complete second configuration file and use it via the --config-file <filename> command line argument. Then the continuous integration server would use the command npx cypress run --config-file staging. json to use the later configuration file.

How do you set a process environment variable in Cypress?

You can use the configuration API and do something like this on your plugins file. Set config. env = process. env which will set your entire node env for Cypress .


2 Answers

A simple workaround: in plugins/index.js do

module.exports = (on, config) => {
  if(config.hasOwnProperty('env') &&  config.env.hasOwnProperty('baseUrl')){
      config.baseUrl = config.env.baseUrl;
  }
  return config
}
like image 126
Wesol Avatar answered Oct 10 '22 01:10

Wesol


After the above post, it was explained in the related github issue that this is not considered a bug. Variables from cypress.env.json are loaded into the environmentVariables variable within the overall config (despite what the current documentation would lead you to believe). The overall config file is cypress.json. In the github issue, I've provided backup concerning why the current explanation is confusing.

like image 36
Dan Swain Avatar answered Oct 10 '22 01:10

Dan Swain