Skip to content
Latchkey

Cypress baseUrl mismatch (CYPRESS_BASE_URL ignored) in CI

Cypress resolves baseUrl from config, the CYPRESS_BASE_URL env var, and --config baseUrl=..., in increasing precedence. When CI sets one but the run reads another, visits target the wrong host, causing connection or unexpected-content failures that look like app bugs.

What this error means

Tests pass against the intended environment locally but in CI hit the wrong host (production instead of preview, or localhost instead of a deploy URL), often surfacing as ECONNREFUSED or wrong-page assertions.

cypress
cy.visit() failed trying to load:

https://staging.example.com/

(expected the preview URL set in CYPRESS_BASE_URL, but the config baseUrl
http://localhost:3000 was used instead)

Common causes

The env var name or scope is wrong

Setting BASE_URL instead of CYPRESS_BASE_URL, or exporting it in a step where Cypress does not run, leaves the config default in effect.

A --config flag overrides the env var

A --config baseUrl=... on the command line takes precedence, silently winning over the env var you set.

How to fix it

Set CYPRESS_BASE_URL in the run step

Export the correctly prefixed variable in the same step that runs Cypress so it overrides the config default.

.github/workflows/ci.yml
- run: npx cypress run
  env:
    CYPRESS_BASE_URL: ${{ steps.deploy.outputs.preview-url }}

Pass baseUrl explicitly and consistently

Use one mechanism: either the env var or --config, not both, so precedence is unambiguous.

Terminal
npx cypress run --config baseUrl=$PREVIEW_URL

How to prevent it

  • Use the CYPRESS_ prefix for env-var overrides.
  • Pick one override mechanism per pipeline to avoid precedence surprises.
  • Echo the resolved baseUrl at run start to confirm the target.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →