Before Spec API

Improve this doc

The before:spec event fires before a spec file is run. The event only fires when running via cypress run.

This is an experimental feature. In order to use it, you must set the experimentalRunEvents configuration option to true.

Syntax

on('before:spec', (spec) => { /* ... */ })

spec (Object)

Details of the spec file, including the following properties:

Property Description
name The base name of the spec file (e.g. login_spec.js)
relative The path to the spec file, relative to the project root (e.g. cypress/integration/login_spec.js)
absolute The absolute path to the spec file (e.g. /Users/janelane/my-app/cypress/integration/login_spec.js)

Usage

You can return a promise from the before:spec event handler and it will be awaited before Cypress proceeds running the spec.

Log the relative spec path to stdout before the spec is run

module.exports = (on, config) => {
  on('before:spec', (spec) => {
    // spec will look something like this:
    // {
    //   name: 'login_spec.js',
    //   relative: 'cypress/integration/login_spec.js',
    //   absolute: '/Users/janelane/app/cypress/integration/login_spec.js',
    // }

    console.log('Running', spec.relative)
  })
}

See also