Skip to content

Propose how to import env variables into Cypress #2447

Open
@dobromir-hristov

Description

@dobromir-hristov

What problem does this feature solve?

Currently there is no way to access the Vue CLI env variables from inside Cypress tests. This is important as sometimes API endpoints are configurable for each environment, local, ci, staging etc.

Cypress has an isolated env from the outside OS inside test files.

What does the proposed API look like?

However, Cypress has a few ways to add variables to its env.

  1. Cypress.json files - this is the current way we do it, and to me this is annoying. I would like to maintain only one set of env files.
  2. CYPRESS_ prefixed env variables. This could work, but the cli loadENV function does not allow for prefixing at this point.
  3. Setting as --env when running Cypress - similar to how we set baseURL when running Cypress. loadEnv does not return the variables, so we cant do that too.
  4. Via plugins - Probably the best way to do it.

When you think about it, you will most probably just need the VUE_APP prefixed ones, as those are the only ones exposed to the Vue App in the first place. With that in mind, we could expose those in methods 3 and 4, to me though, the best way is to add them to plugins/index.js like so:

module.exports = (on, config) => {
  Object.entries(process.env)
    .filter(([key, value]) => key.startsWith('VUE_APP'))
    .forEach(([key, value]) => {
      config.env[key.replace('VUE_APP_', '')] = value
    })

  // this is by default
  return Object.assign({}, config, {
    fixturesFolder: 'tests/e2e/fixtures',
    integrationFolder: 'tests/e2e/specs',
    screenshotsFolder: 'tests/e2e/screenshots',
    videosFolder: 'tests/e2e/videos',
    supportFile: 'tests/e2e/support/index.js'
  })
}

The question is, would anyone actually need ENV vars that are not VUE_APP prefixed? If the CLI loadEnv had a way to return the final resolved env vars map, it would be totally possible.

Users can easily add vars here manually too, as they will be preloaded into the process.env by the CLI.

config.env['APP_VERSION'] = process.env.APP_VERSION

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions