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

Cypress › Extra questions from common lists

How do you debug tests in Cypress?

Locally Cypress is debugged with time travel in the Command Log, cy.pause(), .debug() and DevTools, and in CI with artifacts. The Command Log stores a DOM snapshot for every command, so it shows the page before and after a step. In a headless run pause and debug do not help, so in CI you read the error text, screenshot, video, a log through cy.task and, with Cypress Cloud, Test Replay.

Cypress · Practice and details

The test is green locally and red in CI. Where do you start when you only have a log?

Locally you can look at the screen, in CI you have files and text. So Cypress has two sets of tools: interactive ones for cypress open and artifacts for cypress run. The lesson shows what to reach for and when.

How it works and why

The Test Runner records a DOM snapshot for every command, so the Command Log can show the page state before and after any step. This «time travel» is the main tool. The test runs in an ordinary browser, so DevTools work too: console, network, debugger.

Where to debug

locally

cypress open

Time travel, cy.pause(), .debug(), DevTools, a click on a command.

in CI

cypress run

Screenshots, video (if enabled), logs via cy.task, Test Replay in Cypress Cloud.

Figure 1. Locally you watch live, in CI you read artifacts.
  • Time-travel: hover a command in the Command Log to see the DOM snapshot at that moment.
  • cy.pause(): stops execution, then step through commands.
  • .debug(): sets a debugger and logs the current subject to the console (DevTools must be open).
  • Click on a command: prints details to the console: subject, elements, request and response.
  • cy.log() writes to the Command Log; cy.task('log', ...) prints to the CI terminal.

Example: before → after

Before: in CI «element not found» and no context. After: temporary observation points and a log to the terminal.

cy.get('[data-cy=total]').debug(); // subject in the console + debugger
cy.pause(); // stop, then step manually
cy.get('[data-cy=checkout]').click();

// a log to the CI terminal
cy.url().then((url) => cy.task('log', 'URL: ' + url));
// cypress.config.ts
setupNodeEvents(on) {
  on('task', {
    log(message: string) {
      console.log(message);
      return null; // a task must return something
    },
  });
},

For CI you enable screenshots (by default Cypress takes them on failure in cypress run), video (video: true) and, with Cypress Cloud, Test Replay, where a test can be replayed with DOM and network. Cypress itself logs more with the DEBUG=cypress:* environment variable, but that is for problems with the tool, not with tests.

// your own debugger inside .then(): the subject is available as a parameter
cy.get('[data-cy=total]').then(($el) => {
  debugger; // stops only when DevTools are open
});

// extra context on failure
Cypress.on('fail', (error, runnable) => {
  error.message += '\n' + 'Test: ' + runnable.fullTitle();
  throw error; // without the throw the test would not get the failed state
});

How to read a failed test in CI: first the error text (which command and what was expected), then the screenshot at the moment of failure, then the video to see what led up to it and, with Cypress Cloud, Test Replay with DOM and network. Each next artifact is slower to read, so stop once the cause is clear.

Limits and alternatives

  • cy.pause() and .debug() are of no use in a headless run: in CI they do not help and are removed before committing.
  • Snapshots in the Command Log are snapshots, not a live app: the state after a test can be inspected but the steps cannot be «replayed».
  • Playwright analogues: page.pause(), npx playwright test --debug with the inspector, UI mode (--ui) and Trace Viewer for CI.

Common mistakes

  • Leaving cy.pause() or .debug() in the code: in CI they do nothing but add noise.
  • Opening DevTools after .debug(): the stop has already passed, they had to be opened beforehand.
  • Debugging «by eye» in a single run instead of saving artifacts and reproducing the error.

Check yourself

A test fails only in CI and the team suggests adding cy.pause(). Why will that not help?

Answer

In CI the run is headless without an interactive Command Log, so a pause shows nothing. Artifacts are needed: screenshot, video, a log via cy.task or Test Replay.

How does .debug() differ from cy.log()?

Answer

.debug() stops in DevTools and prints the subject to the console, while cy.log() only writes a message to the Command Log.

In an interview. Junior: Locally I use time travel in the Command Log, cy.pause for stepping and .debug() to inspect the subject in DevTools. Middle/Senior: In CI I look at screenshots, video, logs through cy.task and Test Replay. I tell interactive tools from artifacts. In Playwright the analogue is Trace Viewer.

More articles in this chapter