From e00a2112d500798e18a2ce3b373e216a3e98413b Mon Sep 17 00:00:00 2001 From: Cacie Prins Date: Tue, 3 Jun 2025 10:42:39 -0400 Subject: [PATCH 1/2] clarify that dynamic values cannot be provided to Cypress.require --- docs/api/cypress-api/require.mdx | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/api/cypress-api/require.mdx b/docs/api/cypress-api/require.mdx index 66b38255b5..928ea97d09 100644 --- a/docs/api/cypress-api/require.mdx +++ b/docs/api/cypress-api/require.mdx @@ -28,7 +28,7 @@ callback requires enabling the ::: ```js -Cypress.require(moduleNameOrPath) +Cypress.require('moduleNameOrPath') ``` ## Usage @@ -57,6 +57,12 @@ cy.origin('cypress.io', async () => { const _ = require('lodash') const utils = await import('./utils') }) + +// `Cypress.require` must be passed a static string; dynamic values will not +// import correctly. +cy.origin('cypress.io', { args: { lodash: 'lodash' } }, ({ lodash }) => { + const _ = Cypress.require(lodash) +}) ``` See @@ -109,7 +115,7 @@ cy.origin('cypress.io', async () => { - `.mjs` - `.json` - `.coffee` -- `Cypress.require('dependency-name')` must on one line as a continuous string: +- `Cypress.require('dependency-name')` must on one line as a continuous string, and the dependency cannot be dyanmically defined: {/* prettier-ignore-start */} ```js @@ -131,6 +137,11 @@ Cypress . require('lodash') Cypress.require( 'lodash' ) + +// ❌ BAD +const lodashPkgName = 'lodash' +Cypress.require(lodashPkgName) + ``` {/* prettier-ignore-end */} From 1fd9a10ed52b82706679d022705ccea110461fb8 Mon Sep 17 00:00:00 2001 From: Cacie Prins Date: Tue, 3 Jun 2025 15:05:14 -0400 Subject: [PATCH 2/2] move comment on incorrect usage example, add icons --- docs/api/cypress-api/require.mdx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/docs/api/cypress-api/require.mdx b/docs/api/cypress-api/require.mdx index 928ea97d09..b10753509d 100644 --- a/docs/api/cypress-api/require.mdx +++ b/docs/api/cypress-api/require.mdx @@ -47,20 +47,19 @@ cy.origin('cypress.io', () => { **Incorrect Usage** ```js -// `Cypress.require()` cannot be used outside the `cy.origin()` callback. +// ❌ `Cypress.require()` cannot be used outside the `cy.origin()` callback. // Use CommonJS `require()` instead const _ = Cypress.require('lodash') cy.origin('cypress.io', async () => { - // `require()` and `import()` cannot be used inside the `cy.origin()` callback. + // ❌ `require()` and `import()` cannot be used inside the `cy.origin()` callback. // Use `Cypress.require()` instead const _ = require('lodash') const utils = await import('./utils') }) -// `Cypress.require` must be passed a static string; dynamic values will not -// import correctly. cy.origin('cypress.io', { args: { lodash: 'lodash' } }, ({ lodash }) => { + // ❌ Dynamic values and variable references cannot be passed to `Cypress.require`. const _ = Cypress.require(lodash) }) ```