Recently Cypress announced a change in how it will handle environment variables. Variables were always public and accessible to the application under test in the browser (via calling Cypress.env() method), but now you can have truly private values. I published a blog post Migrating From Cypress.env To cy.env and Cypress.expose Methods describing the change.
Looking at the changes, I noticed a missing area: it is not easy to pass public variables (non-secrets) to Cypress tests. I used a lot of "configuration" values that were meant to be used inside the spec files, and now these values are private by default. For example, let's say we want to run a test only on CI. We could do something like this:
1 | // only run these tests on CI |
In Cypress before v15 we could have set / used "ci" and "ciName" via environment variables, for example, if using Cypress github action:
1 | name: ci |
The test runs, Cypress.env() returns { ci: true, ciName: 'GitHub Actions' }, everyone is happy. But in Cypress v16 the process environment variables that start with CYPRESS_ are automatically added to the private cy.env space, and are not accessible outside test blocks / hooks. We could still pass them, but now we need to use a complete custom cypress run --expose ... command, instead of letting Cypress GitHub Action do its thing!
This is why I wrote cypress-expose plugin. It grabs every CYPRESS_EXPOSE_... process environment variable and sets it into Cypress.expose object, letting the specs access it. This is meant for public data only! Simply add plugin to your setupNodeEvents callback function and pass the config object.
1 | const { defineConfig } = require('cypress') |
Boom, you are all set. Now any CYPRESS_EXPOSE_... variable is available under camel-cased name. Let's say we run Cypress using the following command:
1 | CYPRESS_API_KEY=123secret! \ |
The api key and the password are going to be reachable via cy.env command, while the answer and the username are publicly accessible using Cypress.expose:

The EXPOSE_ANSWER value in the cy.env is null, the plugin overwrites it, but cannot delete it (since Cypress merged any modifications to the env object with the original values). I feel like having null there is enough.
Take the plugin for a spin, let me know if it works for you.