Cypress · Browser testing end to end

How do assertions work in Cypress?

MediumassertionschaishouldUpdated Oct 2026
Quiz this track

Short answer

Cypress assertions use Chai and come in implicit forms such as .should and explicit forms such as expect. Implicit assertions are retried with the command, while explicit assertions inside .then run once.

Cypress supports both implicit and explicit assertions. Implicit assertions are chained onto a command, such as .should("be.visible"), and Cypress retries them until they pass or time out. Explicit assertions use expect or assert inside a callback and are not retried by the command queue.

ts
cy.get("[data-cy=cart-count]")
  .should("be.visible")
  .and("have.text", "3");

cy.get("[data-cy=cart-item]").then((items) => {
  expect(items).to.have.length(3);
});
The chained should is retried; the expect inside then runs on the value at that moment.

Implicit versus explicit

  • Use .should and .and for DOM state that may arrive asynchronously
  • Use expect inside .then when you need to inspect a value in JavaScript
  • Return the .then chain if it contains assertions, so failures are attributed correctly
  • Use .should with a callback for custom assertions on the subject
  • Prefer user-visible assertions over internal implementation details
ts
cy.get("[data-cy=total]").should(($total) => {
  const value = Number($total.text().replace("$", ""));
  expect(value).to.be.greaterThan(0);
});
A callback assertion can transform the subject, but it is still retried because it is inside should.
An assertion that depends on a value fetched earlier will not retry that fetch. If the underlying data changes, query again inside the assertion or use an intercept and wait for the response.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What is the difference between should and expect?
  2. 2Which assertions are retried?
  3. 3How do you assert on a computed value?
  4. 4Why should assertions avoid implementation details?

Say it in the interview

Cypress uses Chai for assertions. I use chained should for anything that may appear asynchronously because Cypress retries it with the command. I use expect inside then when I need ordinary JavaScript on the subject. I keep assertions focused on what the user can see, and I return the then chain so a failure is reported against the right command.

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.