How do you configure different environments and environment variables?
Cypress environments are set with variables: secrets are read with cy.env(), public values with Cypress.expose(); Cypress.env() was removed in 16. Secrets come from the env block of the config, a cypress.env.json file outside git, OS variables with the CYPRESS_ prefix or the --env flag, public values from the expose key or --expose. The environment is chosen with a flag, and baseUrl is set in setupNodeEvents.
Cypress · Patterns and data
The test is green on dev, and on staging it logs in with someone else’s password. Where does Cypress take the values from?
The same suite runs locally, on dev, on staging and in CI. The app address, login and password differ each time, and the password may be neither committed nor printed in logs. Cypress has several sources of values for this, and when there are several, one of them wins. This lesson covers how the sources work, how secret and public values differ and how to switch environments without editing tests.
How it works and why. Values are usually set in four places (the fifth, setupNodeEvents, appears in the example below): the env block in cypress.config.ts, the cypress.env.json file, OS environment variables with the CYPRESS_ prefix and the --env flag. When a name matches, the later source in the list wins: the flag overrides the OS variable, the variable overrides the file, the file overrides the config. This is convenient: the config holds safe defaults and CI overrides them with its own.
Reading a value in a test now takes two paths. Secrets are taken asynchronously with cy.env(['NAME']): the command yields an object with the requested keys, so the value does not sit in the general config object. Public settings, such as an API address, are read synchronously with Cypress.expose('NAME'). The old Cypress.env() was removed in Cypress 16, so code from old examples on the web needs rewriting.
Who wins when a name matches
Example: from hard-coded values to environments. Before: the address and password right in the test, with a copy for staging in another file.
cy.visit('https://dev.example.com/login');
cy.get('[data-cy=email]').type('anna@example.com');
cy.get('[data-cy=password]').type('P@ssw0rd-from-dev');
After: the config picks baseUrl by environment name and the test takes secrets from cy.env. If the environment name is wrong, it is better to stop at once than to silently get undefined.
// cypress.config.ts
import { defineConfig } from 'cypress';
const urls: Record<string, string> = {
dev: 'https://dev.example.com',
staging: 'https://staging.example.com',
};
export default defineConfig({
expose: { apiUrl: '/api' },
e2e: {
setupNodeEvents(on, config) {
const name = String(config.env.envName ?? 'dev');
if (!urls[name]) throw new Error('Unknown environment: ' + name);
config.baseUrl = urls[name];
return config;
},
},
});
// the test
cy.visit('/login');
cy.env(['USER_EMAIL', 'USER_PASSWORD']).then(({ USER_EMAIL, USER_PASSWORD }) => {
cy.request('POST', Cypress.expose('apiUrl') + '/auth/login', {
email: USER_EMAIL,
password: USER_PASSWORD,
});
});
# CI: the secret comes from the pipeline store
CYPRESS_USER_EMAIL=ci-bot@example.com \
CYPRESS_USER_PASSWORD="$STAGING_PASSWORD" \
npx cypress run --env envName=staging
Why this way. The test does not know where it runs: the outside world defines the environment. The secret is written nowhere in the repository: locally it sits in cypress.env.json (this file goes into .gitignore), in CI in a variable from the secret store. Besides --env, you can switch environments with a separate config: --config-file cypress.staging.config.ts, or override baseUrl with the CYPRESS_BASE_URL variable.
Limits and alternatives. Hiding a secret in Cypress does not replace discipline: a value in --env is visible in the process list, and cy.log() or type() without { log: false } prints it to the command log and the video. Prefer dedicated test accounts with limited rights over real ones. In Playwright variables are plain process.env, usually with dotenv, and environments are set through use.baseURL or separate projects.
Common mistakes.
- Committing
cypress.env.jsonwith a password: the secret stays in git history even after the file is deleted. - Using
Cypress.env()from old examples: it is gone in Cypress 16. - A typo in a CI variable name (
CYPRESS_USER_PASWORD): the value is empty and the test fails at login, not at configuration. - Typing a password with
type()without{ log: false }.
Check yourself
The config has env.baseHost = "dev", cypress.env.json has the same field as "staging", and the run uses --env baseHost=prod. Which value does the test see?
Answer
"prod": the --env flag is stronger than the OS variable, the file and the config.
How do cy.env() and Cypress.expose() differ in purpose and in the way of reading?
Answer
cy.env is for secrets: an asynchronous command that yields only the requested keys. Cypress.expose is for public settings: a synchronous call, values visible to tests and the app.