Skip to content

Cypress should Assertion Not Working Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Cypress should Assertion Not Working Fix. We cover key concepts, practical examples, and best practices.

Your cy.get().should() assertion fails even though the element looks correct in the browser — or the assertion passes when it shouldn't.

The Problem

// WRONG — using expect() instead of should() for chained assertions
cy.get('.status').then(($el) => {
  expect($el.text()).to.equal('Active');
  // This works but doesn't retry — fails if text updates asynchronously
});

expect() inside .then() runs once without retrying. If the element's text updates after a short delay, the assertion fails.

Step-by-Step Fix

1. Use should() for retry-able assertions

// RIGHT — should() retries automatically
cy.get('.status').should('have.text', 'Active');
cy.get('.status').should('be.visible');
cy.get('.status').should('have.class', 'success');

2. Use should() with callback

// RIGHT — callback for complex assertions
cy.get('.items li').should(($items) => {
  expect($items).to.have.length(3);
  expect($items.first()).to.contain('Item 1');
  expect($items.last()).to.contain('Item 3');
});

3. Chain multiple should() calls

// RIGHT — chained assertions
cy.get('button')
  .should('be.visible')
  .and('be.enabled')
  .and('contain', 'Submit');

4. Use should() with Chainers

// RIGHT — various chai chainers
cy.get('input').should('have.value', '');
cy.get('input').should('have.attr', 'placeholder', 'Enter email');
cy.get('.checkbox').should('be.checked');
cy.get('.progress').should('have.css', 'width', '50%');

Expected output:

  ✓ text assertion retries
  ✓ multiple assertions
  ✓ attribute verification

Prevention Tips

  • Use should() for DOM assertions (retries automatically)
  • Use and() to chain multiple assertions
  • Use should(callback) for complex assertions
  • Use expect() only outside of Cypress command chains
  • Remember: should() retries, expect() does not

Common Mistakes with assert should

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. Mixing let bindings with <- bindings in do notation, producing type errors

These mistakes appear frequently in real-world CYPRESS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What's the difference between should() and expect()?

should() is a Cypress command that retries until the assertion passes or times out. expect() runs once immediately. Use should() inside command chains, expect() in .then() callbacks or non-Cypress code.

Does should() work with all Chai assertions?

Yes. should() supports all Chai BDD chainers: have.text, have.class, be.visible, have.length, have.value, have.css, have.attr, etc.

How do I assert that an element does NOT have a class?

Use .should('not.have.class', 'hidden') or .should('not.be.visible'). The not chainer inverts any assertion.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro