{
  "doc": {
    "id": "api/node-events/after-spec-api",
    "title": "After Spec Event | Cypress Node Events",
    "description": "The after:spec event fires after a spec file is run in Cypress.",
    "section": "api",
    "source_path": "/llm/markdown/api/node-events/after-spec-api.md",
    "version": "3cf5b86b3403f604bdf7f3e35025c3bc3865e02c",
    "updated_at": "2026-05-07T17:44:31.931Z",
    "headings": [
      {
        "id": "api/node-events/after-spec-api#after-spec-event",
        "text": "After Spec Event",
        "level": 1
      },
      {
        "id": "api/node-events/after-spec-api#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/node-events/after-spec-api#usage",
        "text": "Usage",
        "level": 2
      },
      {
        "id": "api/node-events/after-spec-api#log-the-relative-spec-path-to-stdout-after-the-spec-is-run",
        "text": "Log the relative spec path to stdout after the spec is run",
        "level": 3
      },
      {
        "id": "api/node-events/after-spec-api#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/node-events/after-spec-api#delete-the-recorded-video-if-the-spec-passed",
        "text": "Delete the recorded video if the spec passed",
        "level": 3
      },
      {
        "id": "api/node-events/after-spec-api#delete-the-recorded-video-if-no-tests-retried",
        "text": "Delete the recorded video if no tests retried",
        "level": 3
      },
      {
        "id": "api/node-events/after-spec-api#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "content": {
    "type": "root",
    "children": [
      {
        "type": "heading",
        "depth": 1,
        "children": [
          {
            "type": "text",
            "value": "After Spec Event"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The `after:spec` event fires after a spec file is run. When running cypress via `cypress open`, the event will fire when the browser closes."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Syntax"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "⚠️ This code is part of the "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/plugins/plugins-guide.md#Using-a-plugin",
            "children": [
              {
                "type": "text",
                "value": "setupNodeEvents"
              }
            ]
          },
          {
            "type": "text",
            "value": " 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."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "⚠️ When running via `cypress open`, the `after:spec` event only fires if the "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/references/configuration.md#Experiments",
            "children": [
              {
                "type": "text",
                "value": "experimentalInteractiveRunEvents flag"
              }
            ]
          },
          {
            "type": "text",
            "value": " is enabled."
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress.config.js"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress.config.ts"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "const { defineConfig } = require('cypress')module.exports = defineConfig({  // setupNodeEvents can be defined in either  // the e2e or component configuration  e2e: {    setupNodeEvents(on, config) {      on('after:spec', (spec, results) => {        /* ... */      })    },  },})"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "import { defineConfig } from 'cypress'export default defineConfig({  // setupNodeEvents can be defined in either  // the e2e or component configuration  e2e: {    setupNodeEvents(on, config) {      on('after:spec', (spec, results) => {        /* ... */      })    },  },})"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "spec (Object)"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Details of the spec file, including the following properties:"
          }
        ]
      },
      {
        "type": "table",
        "align": [
          null,
          null
        ],
        "children": [
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Property"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Description"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`name`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "The base name of the spec file (e.g. `login.cy.js`)"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`relative`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "The path to the spec file, relative to the project root (e.g. `cypress/e2e/login.cy.js`)"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`absolute`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "The absolute path to the spec file (e.g. `/Users/janelane/my-app/cypress/e2e/login.cy.js`)"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "results (Object)"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Details of the spec file's results, including numbers of passes/failures/etc and details on the tests themselves."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Results are only provided when running via `cypress run`. When running via `cypress open`, the results will be undefined."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Usage"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "You can return a promise from the `after:spec` event handler and it will be awaited before Cypress proceeds with processing the spec's video or moving on to further specs if there are any."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Log the relative spec path to stdout after the spec is run"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress.config.js"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress.config.ts"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "const { defineConfig } = require('cypress')module.exports = defineConfig({  // setupNodeEvents can be defined in either  // the e2e or component configuration  e2e: {    setupNodeEvents(on, config) {      on('after:spec', (spec, results) => {        // spec will look something like this:        // {        //   name: 'login.cy.js',        //   relative: 'cypress/e2e/login.cy.js',        //   absolute: '/Users/janelane/my-app/cypress/e2e/login.cy.js',        // }        // results will look something like this:        // {        //   stats: {        //     suites: 0,        //     tests: 1,        //     passes: 1,        //     pending: 0,        //     skipped: 0,        //     failures: 0,        //     // ...more properties        //   }        //   reporter: 'spec',        //   tests: [        //     {        //       title: ['login', 'logs user in'],        //       state: 'passed',        //       // ...more properties...        //     }        //   ],        //   error: null,        //   video: '/Users/janelane/my-app/cypress/videos/login.cy.js.mp4',        //   screenshots: [],        //   // ...more properties...        // }        console.log('Finished running', spec.relative)      })    },  },})"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "import { defineConfig } from 'cypress'export default defineConfig({  // setupNodeEvents can be defined in either  // the e2e or component configuration  e2e: {    setupNodeEvents(on, config) {      on('after:spec', (spec, results) => {        // spec will look something like this:        // {        //   name: 'login.cy.js',        //   relative: 'cypress/e2e/login.cy.js',        //   absolute: '/Users/janelane/my-app/cypress/e2e/login.cy.js',        // }        // results will look something like this:        // {        //   stats: {        //     suites: 0,        //     tests: 1,        //     passes: 1,        //     pending: 0,        //     skipped: 0,        //     failures: 0,        //     // ...more properties        //   }        //   reporter: 'spec',        //   tests: [        //     {        //       title: ['login', 'logs user in'],        //       state: 'passed',        //       // ...more properties...        //     }        //   ],        //   error: null,        //   video: '/Users/janelane/my-app/cypress/videos/login.cy.js.mp4',        //   screenshots: [],        //   // ...more properties...        // }        console.log('Finished running', spec.relative)      })    },  },})"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Examples"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Delete the recorded video if the spec passed"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "You can delete the recorded video for a spec when certain conditions are met. This will skip the compression and uploading of the video when recording to Cypress Cloud."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The example below shows how to delete the recorded video for specs with no failing tests."
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress.config.js"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress.config.ts"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "const { defineConfig } = require('cypress')const fs = require('fs')module.exports = defineConfig({  // setupNodeEvents can be defined in either  // the e2e or component configuration  e2e: {    setupNodeEvents(on, config) {      on('after:spec', (spec, results) => {        // Do we have failures?        if (results && results.video && results.stats.failures === 0) {          // delete the video if the spec passed          fs.unlinkSync(results.video)        }      })    },  },})"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "import { defineConfig } from 'cypress'import fs from 'fs'export default defineConfig({  // setupNodeEvents can be defined in either  // the e2e or component configuration  e2e: {    setupNodeEvents(on, config) {      on(        'after:spec',        (spec: Cypress.Spec, results: CypressCommandLine.RunResult) => {          // Do we have failures?          if (results && results.video && results.stats.failures === 0) {            // delete the video if the spec passed            fs.unlinkSync(results.video)          }        }      )    },  },})"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Delete the recorded video if no tests retried"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "You can delete the recorded video for a spec when certain conditions are met. This will skip the compression and uploading of the video when recording to Cypress Cloud."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The example below shows how to delete the recorded video for specs that had no retry attempts when using Cypress "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/guides/test-retries.md",
            "children": [
              {
                "type": "text",
                "value": "test retries"
              }
            ]
          },
          {
            "type": "text",
            "value": "."
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress.config.js"
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "cypress.config.ts"
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "const { defineConfig } = require('cypress')const fs = require('fs')module.exports = defineConfig({  // setupNodeEvents can be defined in either  // the e2e or component configuration  e2e: {    setupNodeEvents(on, config) {      on('after:spec', (spec, results) => {        if (results && results.video) {          // Do we have failures for any retry attempts?          const failures = results.tests.some((test) =>            test.attempts.some((attempt) => attempt.state === 'failed')          )          if (!failures) {            // delete the video if the spec passed and no tests retried            fs.unlinkSync(results.video)          }        }      })    },  },})"
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "import { defineConfig } from 'cypress'import fs from 'fs'export default defineConfig({  // setupNodeEvents can be defined in either  // the e2e or component configuration  e2e: {    setupNodeEvents(on, config) {      on(        'after:spec',        (spec: Cypress.Spec, results: CypressCommandLine.RunResult) => {          if (results && results.video) {            // Do we have failures for any retry attempts?            const failures = results.tests.some((test) =>              test.attempts.some((attempt) => attempt.state === 'failed')            )            if (!failures) {              // delete the video if the spec passed and no tests retried              fs.unlinkSync(results.video)            }          }        }      )    },  },})"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "See also"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/node-events/before-spec-api.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "Before Spec API"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/node-events/before-run-api.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "Before Run API"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/node-events/after-run-api.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "After Run API"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/plugins/plugins-guide.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "How to use Plugins"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/node-events/overview.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "Node Events Overview"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  "token_estimate": 1389
}