Is the .should('exist') assertion redundant on Cypress?
For your usecase of asserting whether an element exists, they are indeed redundant.
.contains()
yields a DOM element and according to documentation, .should
yields the same element it was given as an input. There are some exceptions when .should yields different element (as you can see in the documentation) but in case of using should('exist')
, they are really redundant
As you mentioned, I personally also prefer adding should
for better readability. Actually I prefer .should('be.visible')
because of following scenario - when an element is hidden or is pushed out of the screen because of some CSS issues, it doesn't exist from user perspective. But..
cy.get('button').contains('Save')
- passes test
cy.get('button').contains('Save').should('exist')
- passes test
cy.get('button').contains('Save').should('be.visible')
- fails test