Cypress · Browser testing end to end
What is the right way to wait in Cypress?
Short answer
Wait on a condition, not on time. Rely on retry-ability for DOM state, alias an intercept and cy.wait on it for network work, and use a bounded timeout only as a safety bound.
An arbitrary cy.wait(2000) guesses how long the app needs. It makes the suite slower when the app is fast and flaky when it is slow. Cypress is designed around conditions: queries retry, assertions retry, and intercepts tell you exactly when a request happened.
cy.intercept("GET", "/api/cart").as("getCart");
cy.get("[data-cy=add-to-cart]").click();
cy.wait("@getCart");
cy.get("[data-cy=cart-count]").should("have.text", "1");Choose the condition
- DOM appears or changes: use a retrying query and should
- A specific request matters: intercept and wait on the alias
- A loading indicator disappears: assert it is not visible, not that time passed
- An animation blocks a click: wait for the final state, or use force only with a reason
- A genuinely fixed delay exists, such as a debounce: assert on the resulting state instead
A timeout is a failure bound, not a synchronization method. If a condition needs 10 seconds, finding out why is more valuable than raising the timeout. Long retries often hide a race between two requests or a shared-state leak.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why is a numeric cy.wait unreliable?
- 2How do you wait for a request that fires on page load?
- 3When is a longer timeout acceptable?
- 4How do you handle a debounced search input?
Say it in the interview
I wait on conditions rather than time: for DOM changes I let Cypress retry the assertion, and for network work I intercept the request, alias it and wait on the alias. A numeric wait guesses and becomes either slow or flaky. Timeouts are safety bounds, so if a test needs a very long one I look for the underlying race instead of raising it.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Cypress mock interview and get scored feedback on depth, clarity and confidence.