After Spec API
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.
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 after:spec
event only fires if the
experimentalInteractiveRunEvents flag
is enabled.
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) => {
/* ... */
})
}
}
})
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) => {
/* ... */
})
}
}
})
Deprecated
The plugins file is deprecated as of Cypress CFG_VERSION. We recommend that you update your configuration. Please see the plugins guide and the migration guide for more information.
// cypress/plugins/index.js
module.exports = (on, config) => {
on('after:spec', (spec, results) => {
/* ... */
})
}
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/e2e/login_spec.js ) |
absolute | The absolute path to the spec file (e.g. /Users/janelane/my-app/cypress/e2e/login_spec.js ) |
results (Object)
Details of the spec file's results, including numbers of passes/failures/etc and details on the tests themselves.
Results are only provided when running via cypress run
. When running via
cypress open
, the results will be undefined.
Usage
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.
Log the relative spec path to stdout after the spec is run
module.exports = (on, config) => {
on('after:spec', (spec, results) => {
// spec will look something like this:
// {
// name: 'login_spec.js',
// relative: 'cypress/e2e/login_spec.js',
// absolute: '/Users/janelane/my-app/cypress/e2e/login_spec.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',
// body: 'function () {}',
// // ...more properties...
// }
// ],
// error: null,
// video: '/Users/janelane/my-app/cypress/videos/login_spec.js.mp4',
// screenshots: [],
// // ...more properties...
// }
console.log('Finished running', spec.relative)
})
}
Examples
Delete the recorded video if the spec passed
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 the Dashboard.
The example below shows how to delete the recorded video for specs with no failing tests.
const { defineConfig } = require('cypress')
// need to install the "del" module as a dependency
// npm i del --save-dev
const del = require('del')
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.stats.failures === 0 && results.video) {
// `del()` returns a promise, so it's important to return it to ensure
// deleting the video is finished before moving on
return del(results.video)
}
})
}
}
})
import { defineConfig } from 'cypress'
// need to install the "del" module as a dependency
// npm i del --save-dev
const del = require('del')
export default defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('after:spec', (spec, results) => {
if (results && results.stats.failures === 0 && results.video) {
// `del()` returns a promise, so it's important to return it to ensure
// deleting the video is finished before moving on
return del(results.video)
}
})
}
}
})
Deprecated
The plugins file is deprecated as of Cypress CFG_VERSION. We recommend that you update your configuration. Please see the plugins guide and the migration guide for more information.
// cypress/plugins/index.js
// need to install the "del" module as a dependency
// npm i del --save-dev
const del = require('del')
module.exports = (on, config) => {
on('after:spec', (spec, results) => {
if (results && results.stats.failures === 0 && results.video) {
// `del()` returns a promise, so it's important to return it to ensure
// deleting the video is finished before moving on
return del(results.video)
}
})
}
Delete the recorded video if no tests retried
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 the Dashboard.
The example below shows how to delete the recorded video for specs that had no retry attempts when using Cypress test retries.
const { defineConfig } = require('cypress')
// need to install these dependencies
// npm i lodash del --save-dev
const _ = require('lodash')
const del = require('del')
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 = _.some(results.tests, (test) => {
return _.some(test.attempts, { state: 'failed' })
})
if (!failures) {
// delete the video if the spec passed and no tests retried
return del(results.video)
}
}
})
}
}
})
import { defineConfig } from 'cypress'
// need to install these dependencies
// npm i lodash del --save-dev
const _ = require('lodash')
const del = require('del')
export default 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 = _.some(results.tests, (test) => {
return _.some(test.attempts, { state: 'failed' })
})
if (!failures) {
// delete the video if the spec passed and no tests retried
return del(results.video)
}
}
})
}
}
})
Deprecated
The plugins file is deprecated as of Cypress CFG_VERSION. We recommend that you update your configuration. Please see the plugins guide and the migration guide for more information.
// cypress/plugins/index.js
// need to install these dependencies
// npm i lodash del --save-dev
const _ = require('lodash')
const del = require('del')
module.exports = (on, config) => {
on('after:spec', (spec, results) => {
if (results && results.video) {
// Do we have failures for any retry attempts?
const failures = _.some(results.tests, (test) => {
return _.some(test.attempts, { state: 'failed' })
})
if (!failures) {
// delete the video if the spec passed and no tests retried
return del(results.video)
}
}
})
}