vages
vages

Reputation: 348

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?

Upvotes: 8

Views: 8662

Answers (2)

Dan Swain
Dan Swain

Reputation: 3108

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.

Upvotes: 5

Wesol
Wesol

Reputation: 165

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
}

Upvotes: 11

Related Questions