Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions content/_changelogs/10.11.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## 10.11.0

_Released 10/25/2022_

**Features:**

- `cy.origin()` now supports using `require()` and dynamic `import()` to include
dependencies. `Cypress.require()` has been removed. Addresses
[#24293](https://github.com/cypress-io/cypress/issues/24293).
4 changes: 0 additions & 4 deletions content/_data/sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -985,10 +985,6 @@
"title": "platform",
"slug": "platform"
},
{
"title": "require",
"slug": "require"
},
{
"title": "session",
"slug": "session"
Expand Down
56 changes: 42 additions & 14 deletions content/api/commands/origin.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,27 +507,53 @@ into the callback.

### Dependencies / Sharing Code

It is not possible to use
[ES module dynamic `import()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#dynamic_imports)
and/or
[CommonJS `require()`](https://nodejs.org/en/knowledge/getting-started/what-is-require/)
or
[dynamic ES module `import()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#dynamic_imports)
within the callback. However, [`Cypress.require()`](/api/cypress-api/require)
can be utilized to include [npm](https://www.npmjs.com/) packages and other
files. It is functionally the same as using
[CommonJS `require()`](https://nodejs.org/en/knowledge/getting-started/what-is-require/)
in browser-targeted code.
can be used within the callback to include [npm](https://www.npmjs.com/)
packages and other files.

<Alert type="warning">

Using `import()` and `require()` within the callback requires version 5.15.0 or
greater of the
[`@cypress/webpack-preprocessor`](https://github.com/cypress-io/cypress/tree/master/npm/webpack-preprocessor).
This is included in Cypress by default, but if your project installs its own
version of `@cypress/webpack-preprocessor` that is set up in your Cypress
config, make sure it is version 5.15.0 or greater.

If using an older version of the webpack or a different preprocessor, you'll see
an error that includes the following text:

_Using require() or import() to include dependencies requires using the latest
version of @cypress/webpack-preprocessor._

</Alert>

#### Example

```js
// ES modules
cy.origin('somesite.com', async () => {
const _ = await import('lodash')
const utils = await import('../support/utils')

// ... use lodash and utils ...
})

// CommonJS
cy.origin('somesite.com', () => {
const _ = Cypress.require('lodash')
const utils = Cypress.require('../support/utils')
const _ = require('lodash')
const utils = require('../support/utils')

// ... use lodash and utils ...
})
```

`Cypress.require()` can be used to share custom commands between tests run in
primary and secondary origins. We recommend this pattern for setting up your
#### Custom commands

This makes it possible to share custom commands between tests run in primary and
secondary origins. We recommend this pattern for setting up your
[support file](/guides/core-concepts/writing-and-organizing-tests#Support-file)
and setting up custom commands to run within the `cy.origin()` callback:

Expand Down Expand Up @@ -561,7 +587,7 @@ before(() => {
// calls in this spec. put it in your support file to make them available to
// all specs
cy.origin('somesite.com', () => {
Cypress.require('../support/commands')
require('../support/commands')
})
})

Expand All @@ -573,6 +599,8 @@ it('tests somesite.com', () => {
})
```

#### Shared execution context

The JavaScript execution context is persisted between `cy.origin()` callbacks
that share the same origin. This can be utilized to share code between
successive `cy.origin()` calls.
Expand All @@ -582,7 +610,7 @@ before(() => {
cy.origin('somesite.com', () => {
// makes commands defined in this file available to all callbacks
// for somesite.com
Cypress.require('../support/commands')
require('../support/commands')
})
})

Expand Down
77 changes: 0 additions & 77 deletions content/api/cypress-api/require.md

This file was deleted.

1 change: 0 additions & 1 deletion cypress/fixtures/sidebar-overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"/api/cypress-api/iscy": "Cypress.isCy",
"/api/cypress-api/cypress-log": "Cypress.log",
"/api/cypress-api/platform": "Cypress.platform",
"/api/cypress-api/require": "Cypress.require",
"/api/cypress-api/spec": "Cypress.spec",
"/api/cypress-api/session": "Cypress.session",
"/api/cypress-api/testing-type": "Cypress.testingType",
Expand Down