Run all assertions of a test in Cypress

Viewed 1287

I've a question regarding test structure. The docs clearly state to write tests with multiple assertions instead of multiple tests with single assertions.

But if I want to test if multiple elements are visible the test will fail if the first element was not visible and I therefore have no clue about the other elements. Is there anything I can do to run all assertions?

it("Page should show two actions", () => {
  cy.visit("users/list");
  cy.get("[data-cy=createUser]")
    .should("be.visible")
    .and("have.text", "Create user");
  cy.get("[data-cy=exportUserData]")
    .should("be.visible")
    .and("have.text", "Export data");
});
2 Answers

To run all the assertions you have to make sure the elements are visible. Usually while running cypress it takes time to load everything, between each assertion you can wait, by using this function cy.wait(2000)

Apparently I just didn't find the correct wording for this. Of course it is called soft assertion and not build into Cypress and actually against Chai's assertion philosophy. There are workarounds although I couldn't really find official documentation of Cypress or Chai that could tell me why they approached against soft assertions.

What you can do is:

  • Use the npm package soft-assert as mentioned here
  • Implement it yourself as mentioned here (can break Cypress' retry/timeout mechanism)
Related