Before Run Event
The before:run event fires before a run starts. When running cypress via
cypress open, the event fires when opening a project.
The event fires each time cypress run executes. As a result, if running
your specs in parallel, the
event fires once for each machine that runs the tests.
Syntax
⚠️ This code is part of the
setupNodeEvents function and
thus executes in the Node environment. You cannot call Cypress or cy
commands in this function, but you do have the direct access to the file system
and the rest of the operating system.
⚠️ When running via cypress open, the before:run event only fires if the
experimentalInteractiveRunEvents flag
is enabled.
- cypress.config.js
- cypress.config.ts
const { defineConfig } = require('cypress')
module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:run', (details) => {
/* ... */
})
},
},
})
import { defineConfig } from 'cypress'
export default defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:run', (details) => {
/* ... */
})
},
},
})
details (Object)
Details of the run, including the project config, system information, and the
version of Cypress. The object includes more details when you run via cypress run.
Usage
You can return a promise from the before:run event handler and Cypress
awaits it before proceeding to run your specs.
Log the browser and the number of specs to run
- cypress.config.js
- cypress.config.ts
const { defineConfig } = require('cypress')
module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:run', (details) => {
// details will look something like this when run via `cypress run`:
// {
// config: {
// projectId: '12345',
// baseUrl: 'http://example.com/',
// viewportWidth: 1000,
// viewportHeight: 660,
// // ...more properties...
// },
// browser: {
// name: 'electron',
// version: '59.0.3071.115',
// // ...more properties...
// },
// system: {
// osName: 'darwin',
// osVersion: '16.7.0',
// }
// cypressVersion: '6.1.0',
// specs: [
// {
// name: 'login_cy.js',
// relative: 'cypress/e2e/login_cy.js',
// absolute: '/Users/janelane/app/cypress/e2e/login_cy.js',
// },
// // ... more specs
// ],
// specPattern: [
// '**/*.cy.{js,jsx,ts,tsx}'
// ],
// If using Cypress Cloud:
// parallel: false,
// group: 'group-1',
// runUrl: 'https://cloud.cypress.io/projects/pjmj9a/runs/22'
// tag: 'tag-1'
// }
// details will look something like this when run via `cypress open`:
// {
// config: {
// projectId: '12345',
// baseUrl: 'http://example.com/',
// viewportWidth: 1000,
// viewportHeight: 660,
// // ...more properties...
// },
// system: {
// osName: 'darwin',
// osVersion: '16.7.0',
// }
// cypressVersion: '7.0.0'
// }
if (details.specs && details.browser) {
// details.specs and details.browser will be undefined in interactive mode
console.log(
'Running',
details.specs.length,
'specs in',
details.browser.name
)
}
})
},
},
})
import { defineConfig } from 'cypress'
export default defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:run', (details) => {
// details will look something like this when run via `cypress run`:
// {
// config: {
// projectId: '12345',
// baseUrl: 'http://example.com/',
// viewportWidth: 1000,
// viewportHeight: 660,
// // ...more properties...
// },
// browser: {
// name: 'electron',
// version: '59.0.3071.115',
// // ...more properties...
// },
// system: {
// osName: 'darwin',
// osVersion: '16.7.0',
// }
// cypressVersion: '6.1.0',
// specs: [
// {
// name: 'login_cy.js',
// relative: 'cypress/e2e/login_cy.js',
// absolute: '/Users/janelane/app/cypress/e2e/login_cy.js',
// },
// // ... more specs
// ],
// specPattern: [
// '**/*.cy.{js,jsx,ts,tsx}'
// ],
// If using Cypress Cloud:
// parallel: false,
// group: 'group-1',
// runUrl: 'https://cloud.cypress.io/projects/pjmj9a/runs/22'
// tag: 'tag-1'
// }
// details will look something like this when run via `cypress open`:
// {
// config: {
// projectId: '12345',
// baseUrl: 'http://example.com/',
// viewportWidth: 1000,
// viewportHeight: 660,
// // ...more properties...
// },
// system: {
// osName: 'darwin',
// osVersion: '16.7.0',
// }
// cypressVersion: '7.0.0'
// }
if (details.specs && details.browser) {
// details.specs and details.browser will be undefined in interactive mode
console.log(
'Running',
details.specs.length,
'specs in',
details.browser.name
)
}
})
},
},
})
Manage the Run Completion Delay
When the recording begins, details.runUrl lets you call the
Run Completion API
before any spec starts. Pass expectedGroupCount so the call is safe from every machine. See the
before:run example.