What types of assertions does Cypress support?
Cypress bundles Chai (BDD and TDD), Chai-jQuery and Sinon-Chai, used implicitly through should or explicitly through expect. An implicit .should() retries together with its query until it passes or times out, while an explicit expect inside .then() runs once. Only .should(callback) retries the whole function, so for a changing DOM use should.
Cypress · Practice and details
The same element: should waits, expect inside then does not. Why?
A test checks the cart counter. Written as .should('have.text', '3') it passes steadily, written as .then(($el) => expect($el.text()).to.eq('3')) it sometimes fails: the value is still updating. Both «check the text»; the difference is whether Cypress repeats the check. This lesson lays out the assertion libraries bundled with Cypress and the two ways to call them.
How it works and why
Cypress ships with several libraries, and their assertions are available through one syntax:
- Chai BDD:
expect(x).to.eq(),.to.include(),.to.have.length(). - Chai TDD:
assert.equal(),assert.isTrue(). - Chai-jQuery: DOM state:
be.visible,have.class,have.text,be.checked. - Sinon-Chai: spies and stubs (
cy.spy,cy.stub):be.called,calledWith.
Two ways to call them. Implicit: .should() and .and() attach to the previous query command. Cypress repeats the query and the assertion together until it passes or the timeout (defaultCommandTimeout, 4 s) expires. Explicit: expect or assert in test code. Inside .then() it runs once on whatever is already there, with no retries. Only .should(callback) repeats the whole function, so explicit expect calls inside it are retried too.
Implicit and explicit assertion
.should(...)
The query and the assertion repeat until success or timeout.
expect у .then
Takes the value at this moment: if it has not updated yet, the test fails.
should chain (or in should(cb)) is retried.Example: before → after
// before: one attempt, flaky
cy.get('[data-cy=cart-count]').then(($el) => {
expect($el.text()).to.eq('3');
});
// after: retried
cy.get('[data-cy=cart-count]').should('have.text', '3');
// several conditions at once: the whole function is repeated
cy.get('[data-cy=row]').should(($rows) => {
expect($rows).to.have.length.greaterThan(2);
expect($rows.first()).to.contain.text('Rex');
});
The function in should(cb) must be pure: no clicks, requests or variable mutations, because it runs as many times as needed to pass.
Limits and alternatives
- The chain of queries before the assertion is retried: the whole chain (
get,find,filter) is recomputed from the first query, but actions (click,type) are not repeated. - Negative assertions:
should('not.exist')passes when the element is not in the DOM, whileshould('not.be.visible')on a missing element fails (since Cypress 6). To check that a spinner is gone usenot.exist;not.be.visiblefits an element that stays in the DOM but is hidden. - For one slow spot raise the timeout of that command, not the global one:
cy.get('[data-cy=report]', { timeout: 15000 }).should('be.visible'). The timeout belongs to the query, and the assertion after it uses it. - A value that is not from the DOM (an API response, a variable) has nothing to retry:
cy.request(...).its('status').should('eq', 200)or.thenwithexpect. - In Playwright the analogue is web-first assertions
await expect(locator).toHaveText(): they retry too, while a manualexpect(await locator.textContent())does not.
Common mistakes
- An explicit
expectinside.thenfor a changing DOM: the test is flaky. Replace withshould. - Side effects in
should(cb)(a click, a counter): the function runs several times. cy.wait(1000)instead of an assertion: a fixed pause is both slower and still flaky.
Check yourself
Why is .then(($el) => expect($el.text()).to.eq('3')) flakier than .should('have.text', '3')?
Answer
then runs once with whatever is in the DOM already, while should repeats the query and the assertion until success or timeout (4 s by default), so it waits for the update.
When is an expect inside a callback retried after all?
Answer
When the callback is passed to .should(cb): Cypress repeats the whole function until no expect throws.
should, which retry together with the query, and an explicit expect in then, which runs once. For several conditions I use should(cb) without side effects. It is the same principle as web-first assertions in Playwright.