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

Cypress › Outdated-answer traps

How do you make a pause (sleep) in Cypress?

Technically it is cy.wait(ms), but a fixed pause guesses the time, so the right approach is to wait for a state, not for time. Query commands and should assertions retry until the timeout, and for the network the test waits on an alias from cy.intercept. A long pause wastes seconds on every run, a short one fails on slow CI. Where time is the requirement (debounce), control it with cy.clock() and cy.tick().

Cypress · Waiting

The button appears after a second or two. Put cy.wait(2000) and forget it?

Technically cy.wait(ms) does pause, which is why the question is tricky. In an interview show that you know the catch: a fixed pause guesses the time. Guess generously and the test wastes seconds on every run; guess short and it fails on a slow CI machine.

How it works

Query commands (cy.get, cy.contains) and should assertions retry until they pass or the timeout runs out (defaultCommandTimeout is 4 s by default). So Cypress already waits, but it waits for a state, not for time: as soon as the condition holds, the test moves on. For the network the state is the end of a specific request: cy.intercept with an alias and cy.wait('@alias').

Pause or state

guessing

cy.wait(2000)

Always 2 s, even when ready in 100 ms. On a slow CI it may not be enough.

waiting for a state

should('be.visible')

Moves on as soon as the condition holds; fails only after the timeout.

Figure 1. Fixed time against waiting for a condition.

Example: from a pause to a wait

// Before
cy.get('[data-cy=search]').type('cypress');
cy.wait(2000);
cy.get('[data-cy=result]').should('have.length', 5);
// After: wait for the request, then the state
cy.intercept('GET', '/api/search*').as('search');
cy.get('[data-cy=search]').type('cypress');
cy.wait('@search');
cy.get('[data-cy=result]').should('have.length', 5);

The intercept with an alias is set before the action that triggers the request, otherwise it may miss it. The assertion on list length already retries on its own, so an extra wait is not always needed.

Limits and alternatives

The rare justified use: checking a debounce or delay, where the time itself is the requirement. Even then it is better to control time with cy.clock() and cy.tick() than to wait for real. In Playwright the same antipattern is page.waitForTimeout(), and web-first assertions and waitForResponse replace it.

Three kinds of waiting

It helps to distinguish what exactly the test waits for. An element state: should('be.visible'), should('have.text', …); all of it retries up to defaultCommandTimeout (4 s). A network event: cy.wait('@alias') after cy.intercept; here requestTimeout and responseTimeout apply. Time as a requirement: debounce, polling, a timer on the page; it is run through cy.clock() and cy.tick() without real waiting.

// Debounce of 500 ms: checked without a real pause
cy.clock();
cy.visit('/search');
cy.get('[data-cy=search]').type('cyp');
cy.get('@searchCalls.all').should('have.length', 0);
cy.tick(500);                  // 500 ms have "passed"
cy.wait('@searchCalls');
cy.get('@searchCalls.all').should('have.length', 1);

In this example searchCalls is an alias on cy.intercept('GET', '/api/search*').as('searchCalls'), registered before the visit. cy.clock() replaces the page timers, so time moves only through cy.tick: the test is instant and does not depend on machine speed. Order matters: call cy.clock() before the app creates its timers.

Another catch: after cy.wait('@alias') the UI may not have updated yet, because the response arrived but rendering continues. So the request wait is followed by an assertion on the visible result, not straight by the next action.

Common mistakes

  • cy.wait before every click "just in case": the suite grows by minutes.
  • Raising the pause when a test fails: that treats the symptom and keeps the race.
  • Setting cy.intercept after the action: the request has already gone and cy.wait('@alias') never gets it.

Check yourself

A test with cy.wait(1000) is stable locally but fails on CI. What does that say about the pause?

Answer

The second was guessed for a fast machine. On CI the response takes longer. Wait for a state: an assertion or a request alias.

When is cy.wait(ms) appropriate?

Answer

Almost never: only when time is part of the requirement (debounce), and then cy.clock with cy.tick is better.

In the interview. Junior: Formally it is cy.wait with milliseconds, but I do not do that: it is a guessed number and the test either waits for nothing or fails. Middle/Senior: I wait for a concrete state: a retrying assertion or the end of a request through an intercept with an alias. For debounce I control time through cy.clock.

More articles in this chapter