Soft Assertion in Cypress

Profile picture for user arilio666

Although cypress includes Chai, Chai-Jquery, and Sinon-Chai assertions, it typically fails the test if the condition is not satisfied then and there.

Traditional assertions in testing frameworks are "hard," meaning they stop the test immediately upon failure.  This can be useful for quickly identifying and fixing issues in the code. Still, gathering information about multiple failures in a single test run can also make it challenging. 

In contrast, soft assertions allow the test to continue running even after one or more assertions have failed so that the full extent of the failures can be identified.

For example, let us run an assertion using cypress's built-in.

       cy.visit('https://www.programsbuzz.com/user/login')
     
       cy.get('#edit-name').type('Naruto').should('have.length', 77)
       cy.get('#edit-pass').type('uzumaki')
  • We can see here that the length should be one, but here, we expected it to be 77, and it failed, and it never even went to the next step.
  • This is why other test steps are not run, and we cannot identify the other errors.
    With soft assertion, we can resolve this.

npm i -D cypress-soft-assertions
  • Install the above plugin for cypress that installs soft assertion.
  • After installation, import it into your test.
  • To add a soft assertion instead of .should() .better() is used in the soft assertion.
  • Now let us run the same test instead of should.
cy.visit('https://www.programsbuzz.com/user/login')
     
       cy.get('#edit-name').type('Naruto').better('have.length', 77)
       cy.get('#edit-pass').type('uzumaki')
  • We can see here instead of stopping the entire test, and we can see that it continued and finished the other steps.

Comments

Hi, thank You for this advice. Unfortunately, when I try to install this plugin I get the Error message that it could not resolve the dependency  peer cypress@"11" from cypress-soft-assertions@1.1.0 since the Cypress 12.11.0 is installed. Any help with this?scrn sht