Skip to content

Commit c301833

Browse files
committed
feat: add Cypress.expose() API
1 parent 11b424e commit c301833

File tree

36 files changed

+388
-54
lines changed

36 files changed

+388
-54
lines changed

.circleci/src/pipeline/@pipeline.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ commands:
127127
if [[ "<< pipeline.parameters.force-persist-artifacts >>" == "true" ]]; then
128128
echo 'export SHOULD_PERSIST_ARTIFACTS=true' >> "$BASH_ENV"
129129
else
130-
echo 'if ! [[ "$CIRCLE_BRANCH" != "develop" && "$CIRCLE_BRANCH" != "release/"* && "$CIRCLE_BRANCH" != "feat/deprecate_cypress_env_feature_branch" ]]; then
130+
echo 'if ! [[ "$CIRCLE_BRANCH" != "develop" && "$CIRCLE_BRANCH" != "release/"* && "$CIRCLE_BRANCH" != "feat/deprecate_cypress_env_feature_branch" && "$CIRCLE_BRANCH" != "feat/add_cypress_expose" ]]; then
131131
export SHOULD_PERSIST_ARTIFACTS=true
132132
fi' >> "$BASH_ENV"
133133
fi

.circleci/src/pipeline/workflows/@main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ linux-x64:
55
# use the following branch as well to ensure that v8 snapshot cache updates are fully tested
66
- equal: [ 'update-v8-snapshot-cache-on-develop', << pipeline.git.branch >> ]
77
- equal: [ 'feat/deprecate_cypress_env_feature_branch', << pipeline.git.branch >> ]
8+
- equal: [ 'feat/add_cypress_expose', << pipeline.git.branch >> ]
89
- matches:
910
pattern: /^release\/\d+\.\d+\.\d+$/
1011
value: << pipeline.git.branch >>

cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ _Released 01/27/2026 (PENDING)_
1111

1212
- Introduced a new [`cy.env()`](https://docs.cypress.io/api/commands/env) command that can be used to asynchronously and securely access Cypress environment variables. Addressed in [#33181](https://github.com/cypress-io/cypress/pull/33181).
1313
- Added a [`allowCypressEnv`](https://docs.cypress.io/app/references/configuration#Global) configuration option that disallows use of the deprecated `Cypress.env()` API. Addressed in [#33181](https://github.com/cypress-io/cypress/pull/33181).
14+
- Introduced the new `Cypress.expose()` API, intended for use of public configuration of non-sensitive values. Addressed in [#33238](https://github.com/cypress-io/cypress/pull/33238).
1415

1516
**Misc:**
1617

cli/lib/cli.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ const descriptions: any = {
117117
dev: 'runs cypress in development and bypasses binary check',
118118
e2e: 'runs end to end tests',
119119
env: 'sets environment variables. separate multiple values with a comma. overrides any value in cypress.config.{js,ts,mjs,cjs} or cypress.env.json',
120+
expose: 'sets exposed public configuration variables. separate multiple values with a comma. overrides any value in cypress.config.{js,ts,mjs,cjs}',
120121
exit: 'keep the browser open after tests finish',
121122
forceInstall: 'force install the Cypress binary',
122123
global: 'force Cypress into global mode as if it were globally installed',
@@ -252,6 +253,7 @@ const addCypressRunCommand = (program: any): any => {
252253
.option('-C, --config-file <config-file>', text('configFile'))
253254
.option('--e2e', text('e2e'))
254255
.option('-e, --env <env>', text('env'))
256+
.option('-x, --expose <expose>', text('expose'))
255257
.option('--group <name>', text('group'))
256258
.option('-k, --key <record-key>', text('key'))
257259
.option('--headed', text('headed'))
@@ -284,6 +286,7 @@ const addCypressOpenCommand = (program: any): any => {
284286
.option('-d, --detached [bool]', text('detached'), coerceFalse)
285287
.option('--e2e', text('e2e'))
286288
.option('-e, --env <env>', text('env'))
289+
.option('-x, --expose <expose>', text('expose'))
287290
.option('--global', text('global'))
288291
.option('-p, --port <port>', text('port'))
289292
.option('-P, --project <project-path>', text('project'))

cli/lib/exec/open.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ const processOpenOptions = (options: any = {}): string[] => {
4646
args.push('--env', options.env)
4747
}
4848

49+
if (options.expose) {
50+
args.push('--expose', options.expose)
51+
}
52+
4953
if (options.port) {
5054
args.push('--port', options.port)
5155
}

cli/lib/exec/run.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ const processRunOptions = (options: any = {}): string[] => {
7171
args.push('--env', options.env)
7272
}
7373

74+
if (options.expose) {
75+
args.push('--expose', options.expose)
76+
}
77+
7478
if (options.exit === false) {
7579
args.push('--no-exit')
7680
}

cli/lib/util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ const parseOpts = (opts: any): any => {
190190
'e2e',
191191
'exit',
192192
'env',
193+
'expose',
193194
'force',
194195
'global',
195196
'group',

cli/test/lib/__snapshots__/cli.spec.ts.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ exports[`cli > unknown option > shows help 1`] = `
388388
multiple values with a comma. overrides any
389389
value in cypress.config.{js,ts,mjs,cjs} or
390390
cypress.env.json
391+
-x, --expose <expose> sets exposed public configuration variables.
392+
separate multiple values with a comma.
393+
overrides any value in
394+
cypress.config.{js,ts,mjs,cjs}
391395
--global force Cypress into global mode as if it were
392396
globally installed
393397
-p, --port <port> runs Cypress on a specific port. overrides
@@ -530,6 +534,7 @@ exports[`cli > unknown option > shows help for run command 1`] = `
530534
-C, --config-file <config-file> path to script file where configuration values are set. defaults to "cypress.config.{js,ts,mjs,cjs}".
531535
--e2e runs end to end tests
532536
-e, --env <env> sets environment variables. separate multiple values with a comma. overrides any value in cypress.config.{js,ts,mjs,cjs} or cypress.env.json
537+
-x, --expose <expose> sets exposed public configuration variables. separate multiple values with a comma. overrides any value in cypress.config.{js,ts,mjs,cjs}
533538
--group <name> a named group for recorded runs in Cypress Cloud
534539
-k, --key <record-key> your secret Record Key. you can omit this if you set a CYPRESS_RECORD_KEY environment variable.
535540
--headed displays the browser instead of running headlessly

cli/types/cypress.d.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ declare namespace Cypress {
512512
* Returns all environment variables set with CYPRESS_ prefix or in "env" object in "cypress.config.{js,ts,mjs,cjs}"
513513
*
514514
* @see https://on.cypress.io/env
515-
* @deprecated Use {@linkcode Chainable.env cy.env()} instead.
515+
* @deprecated Use {@linkcode Chainable.env cy.env()} or {@linkcode expose Cypress.expose()} instead.
516516
*/
517517
env(): ObjectLike
518518
/**
@@ -522,7 +522,7 @@ declare namespace Cypress {
522522
* // cypress.config.js
523523
* { "env": { "foo": "bar" } }
524524
* Cypress.env("foo") // => bar
525-
* @deprecated Use {@linkcode Chainable.env cy.env()} instead.
525+
* @deprecated Use {@linkcode Chainable.env cy.env()} or {@linkcode expose Cypress.expose()} instead.
526526
*/
527527
env(key: string): any
528528
/**
@@ -531,17 +531,48 @@ declare namespace Cypress {
531531
* @see https://on.cypress.io/env
532532
* @example
533533
* Cypress.env("host", "http://server.dev.local")
534-
* @deprecated Use {@linkcode Chainable.env cy.env()} instead.
534+
* @deprecated Use {@linkcode Chainable.env cy.env()} or {@linkcode expose Cypress.expose()} instead.
535535
*/
536536
env(key: string, value: any): void
537537
/**
538538
* Set values for multiple variables at once. Values are merged with existing values.
539539
* @see https://on.cypress.io/env
540540
* @example
541541
* Cypress.env({ host: "http://server.dev.local", foo: "foo" })
542-
* @deprecated Use {@linkcode Chainable.env cy.env()} instead.
542+
* @deprecated Use {@linkcode Chainable.env cy.env()} or {@linkcode expose Cypress.expose()} instead.
543543
*/
544544
env(object: ObjectLike): void
545+
/**
546+
* Returns all exposed public configuration variables set with --expose in the CLI or in "expose" object in "cypress.config.{js,ts,mjs,cjs}"
547+
*
548+
* @see https://on.cypress.io/expose
549+
*/
550+
551+
expose(): ObjectLike
552+
/**
553+
* Returns specific exposed public configuration variable or undefined
554+
* @see https://on.cypress.io/expose
555+
* @example
556+
* // cypress.config.js
557+
* { "expose": { "foo": "bar" } }
558+
* Cypress.expose("foo") // => bar
559+
*/
560+
expose(key: string): any
561+
/**
562+
* Set value for an exposed public configuration variable.
563+
* Any value you change will be permanently changed for the remainder of your tests.
564+
* @see https://on.cypress.io/expose
565+
* @example
566+
* Cypress.expose("host", "http://server.dev.local")
567+
*/
568+
expose(key: string, value: any): void
569+
/**
570+
* Set values for multiple exposed public configuration variables at once. Values are merged with existing values.
571+
* @see https://on.cypress.io/expose
572+
* @example
573+
* Cypress.expose({ host: "http://server.dev.local", foo: "foo" })
574+
*/
575+
expose(object: ObjectLike): void
545576

546577
/**
547578
* @returns the number of test retries currently enabled for the run
@@ -2974,6 +3005,11 @@ declare namespace Cypress {
29743005
* @default {}
29753006
*/
29763007
env: { [key: string]: any }
3008+
/**
3009+
* Any values to be set as [exposed public configuration variables](https://on.cypress.io/expose).
3010+
* @default {}
3011+
*/
3012+
expose: { [key: string]: any }
29773013
/**
29783014
* A String or Array of glob patterns used to ignore test files that would otherwise be shown in your list of tests. Cypress uses minimatch with the options: {dot: true, matchBase: true}. We suggest using a tool to test what files would match.
29793015
* @default "*.hot-update.js"

cli/types/tests/cypress-tests.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ namespace CypressEnvTests {
5555
})
5656
}
5757

58+
namespace CypressExposeTests {
59+
// Just making sure these are valid - no real type safety
60+
Cypress.expose('foo')
61+
Cypress.expose('foo', 'bar')
62+
Cypress.expose().foo
63+
Cypress.expose({
64+
foo: 'bar',
65+
})
66+
}
67+
5868
namespace CypressStopTests {
5969
Cypress.stop() // $ExpectType void
6070
Cypress.stop('foo') // $ExpectError

0 commit comments

Comments
 (0)