Home > Back-end >  Setting baseUrl in npm script doesn't change it (Cypress)
Setting baseUrl in npm script doesn't change it (Cypress)

Time:11-25

I want to be able to run my Cypress scripts on any URL by modifying the value of baseUrl but the command doesn't change it.

"cypress open --env version=development --config baseUrl=https://google.com"

I have tried env variable too but that also doesn't work:

"cypress:open:dev": "cypress open --env version=development,baseUrl=https://google.com"

Config file:

export default defineConfig({
  e2e: {
    async setupNodeEvents(on, config) {
      const version = config.env.version || 'development'
      const configFile = await import(path.join(
        config.projectRoot,
        'cypress/config',
        `${version}.json`
      ));
      const credentialsFile = await import(path.join(
        config.projectRoot,
        'cypress/config',
        'credentials.json'
      ));
      config = {
        ...config,                    // take config defined in this file
        ...configFile                 // merge/override from the external file
      }
      config.env = {
        ...config.env,                // 2nd level merge
        ...credentialsFile[version]   // from git-ignored file 
      }
      config.baseUrl = configFile.baseUrl
      return config
    },
    reporter: 'mochawesome'
  },
});

development.json:

{
    "env": {
        "baseUrl": "https://test.com",
    }
}

CodePudding user response:

You can use this command in order to set baseUrl:

"cypress:open:dev": "CYPRESS_BASE_URL=https://google.com cypress open --env version=development"

CodePudding user response:

The config gets merged from multiple sources. It would seem that the code in setupNodeEvents() is the final setting, i.e it runs after command line overrides have been applied.

Therefore config.baseUrl = configFile.baseUrl is causing the problem.

I would change it to config.baseUrl = config.baseUrl || configFile.baseUrl.

Since you don't set baseUrl explicitly in the config, it would be undefined if there was no command line override, but have a value if there was.

This change will only assign configFile.baseUrl if there is not a value already.

  • Related