Cypress.require
Cypress.require
is used with cy.origin()
to utilize
dependencies within the [cy.origin()
](/api/commands/ callback function. It
enables the use of npm packages and sharing code from
other files.
It is functionally the same as the
CommonJS require()
used in NodeJs.
Syntax
Cypress.require(moduleId)
Usage
Correct Usage
cy.origin('somesite.com', () => {
const _ = Cypress.require('lodash')
const utils = Cypress.require('./utils')
// ... use lodash and utils ...
})
Incorrect Usage
// `Cypress.require()` cannot be used outside the `cy.origin()` callback.
// Use `require()` instead
const _ = Cypress.require('lodash')
cy.origin('somesite.com', () => {
// `require()` cannot be used inside the `cy.origin()` callback.
// Use `Cypress.require()` instead
const _ = require('lodash')
})
Examples
See cy.origin()
Dependencies / Sharing Code for example usages.
Limitations
Cypress.require
only works when called within thecy.origin()
callback function. It will error if used elsewhere.Cypress.require
only works in conjunction with the webpack preprocessor, which is the default preprocessor for Cypress. If you have manually installed and configured the webpack preprocessor, ensure you are using version5.13.0
or greater.
History
Version | Changes |
---|---|
10.7.0 | Cypress.require added |