QA Interview Hub · Knowledge base · Quizzes · Українська

Cypress › Extra questions from common lists

What Node events does Cypress have, for example after:run?

Cypress Node events are run-level hooks, not test hooks, registered in setupNodeEvents and run in Node, not in the browser. The main ones are before:run and after:run around the whole run, before:spec and after:spec around each spec file, before:browser:launch for browser flags and after:screenshot for processing a screenshot. They are used, for example, to delete videos of passed specs.

Cypress · Practice and details

You want videos of failed tests but not of passed ones. Where is that configured: in the test or in the config?

Some things do not belong to any test: the summary of a whole run, deleting unneeded videos, browser launch flags. For these Cypress has Node events: hooks at the level of a run, not a test. The lesson covers which they are, when they fire and how they differ from beforeEach.

How it works and why

Test hooks (beforeEach) run in the browser. Node events are registered in setupNodeEvents(on, config) and run in the Cypress Node process, which has access to the file system, the network and the full data of the run. That is by design: the browser is isolated, and only Node knows about the «run» as a whole.

Who fires when

run and spec

before:run → before:spec → after:spec → after:run

Around the whole run and every file. Only in cypress run.

targeted

before:browser:launch, after:screenshot

Browser setup before launch, handling each screenshot.

Figure 1. The events before:run, after:run, before:spec, after:spec work in run mode.
  • before:run / after:run: before and after the whole cypress run; after:run receives the results.
  • before:spec / after:spec: around every spec file; after:spec receives that file's results.
  • before:browser:launch: flags, extensions, browser window size.
  • after:screenshot: rename or process a screenshot.

Example: before → after

Before: videos are kept for every spec and the CI artifact is bloated. After: after every spec the video is deleted if it had no failures.

import { defineConfig } from 'cypress';
import { unlinkSync, existsSync } from 'node:fs';

export default defineConfig({
  e2e: {
    video: true,
    setupNodeEvents(on) {
      on('after:spec', (spec, results) => {
        const failed = results.tests.some((test) =>
          test.attempts.some((attempt) => attempt.state === 'failed'),
        );
        if (results.video && !failed && existsSync(results.video)) {
          unlinkSync(results.video); // keep videos of failed specs only
        }
      });
    },
  },
});
on('after:run', (results) => {
  // after a failed launch an error arrives instead of totals
  if ('totalFailed' in results) {
    console.log('Failed tests:', results.totalFailed);
  }
});

The check 'totalFailed' in results is needed because when the launch fails (for example, no specs found) an object with an error message arrives instead of totals. Videos have been off by default since Cypress 13, so video: true must be enabled deliberately.

import fs from 'node:fs';

// browser flags before launch
on('before:browser:launch', (browser, launchOptions) => {
  if (browser.family === 'chromium' && browser.name !== 'electron') {
    launchOptions.args.push('--window-size=1400,900');
  }
  return launchOptions; // return the changed options
});

// rename a screenshot
on('after:screenshot', (details) => {
  const newPath = details.path.replace('.png', '-ci.png');
  fs.renameSync(details.path, newPath); // the handler moves the file itself
  return { path: newPath };
});

before:browser:launch must return launchOptions, otherwise the changes are not applied. after:screenshot does not rename the file itself: the handler moves it (for example with fs.renameSync) and returns the new path so Cypress knows where the file now is.

Limits and alternatives

  • Events do not behave in cypress open the way they do in run mode: check report and notification logic in cypress run.
  • For one event usually a single handler applies: if several plugins register the same event, merge them explicitly, otherwise a later registration replaces the earlier one (on('task') is the exception: Cypress merges task objects).
  • Playwright analogues: globalSetup / globalTeardown in the config or a setup project; run totals in a custom reporter via onEnd.

Common mistakes

  • Registering before:run where cleanup before every test is needed: these are different levels, tests have beforeEach.
  • Writing after:run code that assumes results always holds totals.
  • Deleting a file without checking it exists: the video may not have been created.

Check yourself

How does after:spec differ from after in a spec?

Answer

after runs in the browser, in the test context. after:spec runs in Node after the file finishes and receives its results, video and stats.

Why does the after:run example check 'totalFailed' in results?

Answer

If the launch did not happen, an object with an error arrives instead of totals, and it has no such field.

In an interview. Junior: These are run-level events registered in setupNodeEvents: after:run fires after the whole run and receives the results. Middle/Senior: I use after:spec to delete videos of passed specs and before:browser:launch for browser flags. I remember these events run in Node, not in the browser, and that a handler for one event must be merged across plugins.

More articles in this chapter