{
  "doc": {
    "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": "/llm/markdown/api/node-events/before-spec-api.md",
    "version": "3cf5b86b3403f604bdf7f3e35025c3bc3865e02c",
    "updated_at": "2026-05-07T17:44:31.931Z",
    "headings": [
      {
        "id": "api/node-events/before-spec-api#before-spec-event",
        "text": "Before Spec Event",
        "level": 1
      },
      {
        "id": "api/node-events/before-spec-api#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/node-events/before-spec-api#usage",
        "text": "Usage",
        "level": 2
      },
      {
        "id": "api/node-events/before-spec-api#log-the-relative-spec-path-to-stdout-before-the-spec-is-run",
        "text": "Log the relative spec path to stdout before the spec is run",
        "level": 3
      },
      {
        "id": "api/node-events/before-spec-api#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "chunks": [
    {
      "id": "api/node-events/before-spec-api#syntax",
      "doc_id": "api/node-events/before-spec-api",
      "heading": "Syntax",
      "heading_level": 2,
      "content_markdown": "## Syntax\n\n⚠️ 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.\n\n⚠️ When running via `cypress open`, the `before:spec` event only fires if the [experimentalInteractiveRunEvents flag](/llm/markdown/app/references/configuration.md#Experiments) is enabled.\n\n*   cypress.config.js\n*   cypress.config.ts\n\n```\nconst { 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) => {        /* ... */      })    },  },})\n```\n\n```\nimport { 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) => {        /* ... */      })    },  },})\n```\n\n**spec _(Object)_**\n\nDetails of the spec file, including the following properties:\n\n| Property | Description |\n| --- | --- |\n| `name` | The base name of the spec file (e.g. `login.cy.js`) |\n| `relative` | The path to the spec file, relative to the project root (e.g. `cypress/e2e/login.cy.js`) |\n| `absolute` | The absolute path to the spec file (e.g. `/Users/janelane/my-app/cypress/e2e/login.cy.js`) |\n",
      "section": "api",
      "anchors": [
        "syntax"
      ],
      "path": "/llm/json/chunked/api/node-events/before-spec-api.json",
      "token_estimate": 276
    },
    {
      "id": "api/node-events/before-spec-api#usage",
      "doc_id": "api/node-events/before-spec-api",
      "heading": "Usage",
      "heading_level": 2,
      "content_markdown": "## Usage\n\nYou can return a promise from the `before:spec` event handler and it will be awaited before Cypress proceeds running the spec.\n\n### Log the relative spec path to stdout before the spec is run\n\n*   cypress.config.js\n*   cypress.config.ts\n\n```\nconst { 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)      })    },  },})\n```\n\n```\nimport { 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)      })    },  },})\n```\n",
      "section": "api",
      "anchors": [
        "usage"
      ],
      "path": "/llm/json/chunked/api/node-events/before-spec-api.json",
      "token_estimate": 205
    },
    {
      "id": "api/node-events/before-spec-api#log-the-relative-spec-path-to-stdout-before-the-spec-is-run",
      "doc_id": "api/node-events/before-spec-api",
      "heading": "Log the relative spec path to stdout before the spec is run",
      "heading_level": 3,
      "content_markdown": "### Log the relative spec path to stdout before the spec is run\n\n*   cypress.config.js\n*   cypress.config.ts\n\n```\nconst { 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)      })    },  },})\n```\n\n```\nimport { 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)      })    },  },})\n```\n",
      "section": "api",
      "anchors": [
        "log-the-relative-spec-path-to-stdout-before-the-spec-is-run"
      ],
      "path": "/llm/json/chunked/api/node-events/before-spec-api.json",
      "token_estimate": 175
    }
  ]
}