Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions docs/api/cypress-api/require.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ callback requires enabling the
:::

```js
Cypress.require(moduleNameOrPath)
Cypress.require('moduleNameOrPath')
```

## Usage
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to indicate that this example is one that will not work explicitly

Suggested change
const _ = Cypress.require(lodash)
// will not work as the value is passed dynamically
const _ = Cypress.require(lodash)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, people copy paste bad examples all the time and open an issue about it not working unless we put a gigantic red x next to it 😅

})
```

See
Expand Down Expand Up @@ -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
Expand All @@ -131,6 +137,11 @@ Cypress . require('lodash')
Cypress.require(
'lodash'
)

// ❌ BAD
const lodashPkgName = 'lodash'
Cypress.require(lodashPkgName)

```
{/* prettier-ignore-end */}

Expand Down