---
id: api/node-events/before-spec-api
title: Before Spec Event | Cypress Node Events
description: 'The before:spec event fires before a spec file is run in Cypress.'
section: api
source_path: docs/api/node-events/before-spec-api.mdx
version: 524ff5211e60b5d53e55d6ad976d83966f66e7cd
updated_at: '2026-04-30T14:20:05.396Z'
---
# Before Spec Event

The `before:spec` event fires before a spec file is run. When running cypress via `cypress open`, the event will fire when the browser launches.

## Syntax

⚠️ This code is part of the [setupNodeEvents](/llm/markdown/app/plugins/plugins-guide.md#Using-a-plugin) 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:spec` event only fires if the [experimentalInteractiveRunEvents flag](/llm/markdown/app/references/configuration.md#Experiments) 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:spec', (spec) => {        /* ... */      })    },  },})
```

```
import { defineConfig } from 'cypress'export default defineConfig({  // setupNodeEvents can be defined in either  // the e2e or component configuration  e2e: {    setupNodeEvents(on, config) {      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.cy.js`) |
| `relative` | The path to the spec file, relative to the project root (e.g. `cypress/e2e/login.cy.js`) |
| `absolute` | The absolute path to the spec file (e.g. `/Users/janelane/my-app/cypress/e2e/login.cy.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

*   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:spec', (spec) => {        // spec will look something like this:        // {        //   name: 'login.cy.js',        //   relative: 'cypress/e2e/login.cy.js',        //   absolute: '/Users/janelane/app/cypress/e2e/login.cy.js',        // }        console.log('Running', spec.relative)      })    },  },})
```

```
import { defineConfig } from 'cypress'export default defineConfig({  // setupNodeEvents can be defined in either  // the e2e or component configuration  e2e: {    setupNodeEvents(on, config) {      on('before:spec', (spec) => {        // spec will look something like this:        // {        //   name: 'login.cy.js',        //   relative: 'cypress/e2e/login.cy.js',        //   absolute: '/Users/janelane/app/cypress/e2e/login.cy.js',        // }        console.log('Running', spec.relative)      })    },  },})
```

## See also

*   [After Spec API](/llm/markdown/api/node-events/after-spec-api.md)
*   [Before Run API](/llm/markdown/api/node-events/before-run-api.md)
*   [After Run API](/llm/markdown/api/node-events/after-run-api.md)
*   [How to use Plugins](/llm/markdown/app/plugins/plugins-guide.md)
*   [Node Events Overview](/llm/markdown/api/node-events/overview.md)
