cypress-expose
v1.1.2
Published
Expose public variables via CYPRESS_EXPOSE process variables
Downloads
617
Readme
cypress-expose
Expose public variables via CYPRESS_EXPOSE process variables
Install
$ npm i -D cypress-exposeUse
Any env variable that starts with CYPRESS_EXPOSE will be cast and normalized
const { defineConfig } = require('cypress')
const cypressExpose = require('cypress-expose')
module.exports = defineConfig({
allowCypressEnv: false,
e2e: {
setupNodeEvents(on, config) {
cypressExpose(config)
// IMPORTANT: return the config object
// to let Cypress know we modified it
return config
},
},
})Pass any variables that you are OK with being public using the CYPRESS_EXPOSE prefix:
CYPRESS_EXPOSE_foo=42 npx cypress runThe above execution will have Cypress.expose('foo') // 42
This makes is especially convenient to pass config / CI variables that are not exposed by default
# GitHub Actions
- name: Run tests 🧪
# https://github.com/cypress-io/github-action
uses: cypress-io/github-action@v7
env:
# note: the variables will be normalized to camel case
CYPRESS_EXPOSE_PUBLIC_VARIABLE_1: foo
CYPRESS_EXPOSE_PUBLIC_VARIABLE_2: bar
CYPRESS_EXPOSE_PUBLIC_VARIABLE_3: baz
# default CI variable
CYPRESS_EXPOSE_CI: true
CYPRESS_EXPOSE_CI_NAME: 'GitHub Actions'The above example will have Cypress.expose() include the following object
Cypress.expose()
// {
// publicVariable1: 'foo'
// publicVariable2: 'bar'
// publicVariable3: 'baz'
// ci: true
// ciName: 'GitHub Actions'
// }The same approach works for keys starting with expose inside the env object
const { defineConfig } = require('cypress')
const cypressExpose = require('cypress-expose')
module.exports = defineConfig({
allowCypressEnv: false,
e2e: {
env: {
exposeMe: true,
},
setupNodeEvents(on, config) {
cypressExpose(config)
// IMPORTANT: return the config object
// to let Cypress know we modified it
return config
},
},
})There will be no cy.env('me') value, instead you can get it via Cypress.expose('me') // true
