Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cypress.env prefers value from cypress.json rather than CYPRESS_* env variable #15606

Closed
fnune opened this issue Mar 22, 2021 · 3 comments
Closed

Comments

@fnune
Copy link

fnune commented Mar 22, 2021

Current behavior

Cypress.env("BASE_URL") returns http://localhost:5001 (the value in an environment variable) when running inside tests, but http://localhost:6666 (the value in cypress.json) when running from inside commands.

Desired behavior

Cypress.env("BASE_URL") returns http://localhost:5001 (the environment variable value) both when called from commands as well as tests, instead of returning a different result depending on the runtime.

Test code to reproduce

I have this command:

// cypress/support/commands.ts
Cypress.Commands.add("unauthenticate", () => {
    cy.request("GET", `${Cypress.env("BASE_URL")}/auth/logout`);
});

This cypress.json file:

{
    "env": {
        "BASE_URL": "http://localhost:6666"
    }
}

And I'm calling Cypress like so:

CYPRESS_BASE_URL=http://localhost:5001 yarn cypress:ci

Where the scripts look like this:

        "cypress:ci": "yarn cypress:ci:wait && yarn cypress:run",
        "cypress:ci:wait": "echo \"Waiting for the app at $CYPRESS_BASE_URL...\" && wait-on -t 120000 $CYPRESS_BASE_URL && echo 'Found it!'",
        "cypress:run": "cypress run",

Versions

Cypress package version: 6.8.0
Cypress binary version: 6.8.0
Electron version: 11.3.0
Bundled Node version: 12.18.3
@fnune
Copy link
Author

fnune commented Mar 22, 2021

A workaround (doesn't work: read below) for now:

// commands.ts
const BASE_URL = process.env.CYPRESS_BASE_URL || Cypress.env("BASE_URL");

Elsewhere:

Cypress.env("BASE_URL") // Can use Cypress.env elsewhere

EDIT: the workaround above doesn't work, as it still evaluates to what's in cypress.json. The way to solve it is by just using relative paths in commands:

image

@richardst89
Copy link

I've encountered this issue too.
However, if you include "--env SomeOtherVar=AnyValue" when executing, it appears to re-prioritise BaseUrl from the environment variables.

Definitely something buggy going on!

@jennifer-shehane
Copy link
Member

jennifer-shehane commented May 6, 2021

Duplicate of #8680

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants