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

Cypress › Retry-ability, waiting, timeouts

Why is cy.wait(3000) an antipattern and what to use instead?

A fixed pause like cy.wait(3000) is a guessed number: it knows nothing about application state, so it either slows the test or comes too short. Cypress already waits by itself, so the pause is replaced with a condition: an assertion on UI state, or cy.wait on an alias of a request caught by cy.intercept, registered before the action. Timers are controlled with cy.clock() and cy.tick().

Cypress · Waiting

A test with cy.wait(3000) is green but fails once in ten runs in CI. What do you replace it with?

Someone added cy.wait(3000) after saving because "it did not make it otherwise". Locally it is fine. In CI, where the machine is slower, 3 seconds is sometimes not enough, and when it is enough the test still drags in every run. This lesson explains why a fixed pause loses in both cases and what replaces it.

How it works and why. cy.wait(ms) just stops the test for the given time and knows nothing about the app. Cypress already waits by itself: queries with assertions are retried up to defaultCommandTimeout and actions wait for actionability. So the pause is replaced by waiting for a condition, and the test waits exactly as long as needed, but no longer than the timeout.

A pause or a condition

cy.wait(3000)

A guessed pause

The response came in 0.5 s: 2.5 s lost in every run. It came in 4 s: the test failed. Slow and flaky at once.

Waiting for a state

should / cy.wait(@alias)

The test moves on as soon as the condition holds and waits for it, not for a fixed time.

Figure 1. A pause waits a fixed time, a condition waits for the needed state.

Example: two replacements. There are two reliable ways: a state in the UI or a specific network request.

// before
cy.get('[data-cy=save]').click();
cy.wait(3000);

// 1. wait for a state in the UI
cy.get('[data-cy=save]').click();
cy.get('[data-cy=toast]').should('contain', 'Saved');

// 2. wait for a specific request (intercept BEFORE the action)
cy.intercept('POST', '/api/profile').as('save');
cy.get('[data-cy=save]').click();
cy.wait('@save').its('response.statusCode').should('eq', 200);

The second way requires cy.intercept to be registered before the action: otherwise the request slips past. cy.wait('@save') waits for the request to go out (requestTimeout, 5 s by default) and for the response (responseTimeout, 30 s), then yields the interception you can check. If there are several requests, each next cy.wait('@save') takes the next interception in order.

Limits and alternatives. UI or request: a UI assertion answers "what does the user see", a request answers "did the call go out". The first is better by default, the second is needed when the UI shows no result or you must check the request body. Real time is rarely needed. For debounce, timers and animations, control the time through cy.clock() and cy.tick().

// a 500 ms debounce: control the time instead of waiting for it
cy.clock(); // before cy.visit: the page's timers are faked
cy.visit('/search');
cy.get('[data-cy=search]').type('qa');
cy.tick(500);
cy.get('[data-cy=hint]').should('be.visible');
In Playwright:

The same rule: page.waitForTimeout() is an antipattern. Use a web-first assertion or page.waitForResponse() instead, which, like an alias in Cypress, is set up before the action.

Common mistakes.

  • Replacing cy.wait(3000) with cy.wait(5000) "to be safe": a slower test, the same cause.
  • Registering cy.intercept after the click: the request has gone, and cy.wait times out.
  • Waiting for an alias and not checking the response: the test is green even when the server returned an error.

Check yourself

Why does cy.wait(3000) make a test both slow and flaky?

Answer

Because 3 seconds is a guess. If the app is faster, time is lost in every run; if slower (CI), the test goes on before the app is ready and fails.

cy.wait('@save') fails with "No request ever occurred" although the request is visible in the network. What is the cause?

Answer

Most likely cy.intercept(...).as('save') was registered after the action that sent the request, or the URL pattern or method does not match. Set the intercept before the click and check the method and path.

In an interview. Junior: It is a guessed number: if the app is faster the test waits for nothing, if slower it fails. Instead I wait for a state: an assertion on an element or an intercept alias with cy.wait. Middle/Senior: I choose what to wait for: a UI assertion when what the user sees matters, a request when the call must be checked. I register the intercept before the action. For debounce and timers I use cy.clock and cy.tick, because real time is almost never needed in a test.

More articles in this chapter