DEV Community

Cover image for Cypress Assertions
Dilpreet Johal
Dilpreet Johal

Posted on

7 2

Cypress Assertions

In this tutorial, we will cover different types of Cypress Assertions such as the default, implicit and explicit assertions and talk about the difference between the should and the expect assertions.

Cypress uses the Chai assertion library as well as the extensions of Sinon & jQuery to provide you with dozens of powerful assertions for free.

Default Assertions

Cypress comes with many default assertions that can be used without having to explicitly define assertions, such as –

  • cy.visit(): every-time you visit a page, Cypress expects the page to return with a 200 status code
  • cy.get(): the get command expects the element to exist in the DOM first before trying to access it

Implicit Assertions

The implicit assertions used the should() or the and() commands when making assertions. This is the preferable way of making assertions in Cypress.

implicit1

You can even chain multiple assertions together –

implicit2


Explicit Assertions

You should use Explicit assertions when you would like to make multiple assertions for the same subject or when you would like to manipulate your subject before making your assertion. For explicit assertions, you will use the expect command.

explicit


Check out the video below to see learn more about Cypress Assertions –


📧 Subscribe to my mailing list to get access to more content like this

👍 Follow automationbro on Twitter for the latest updates

...

I love coffees! And, if this post helped you out and you would like to support my work, you can do that by clicking on the button below and buying me a cup of coffee -

Buy me a coffee

You can also support me by liking and sharing this content.

Thanks for reading!

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay