2
votes

`I have configured soft assert from npm (npm i soft-assert) and now my package.josn has "soft-assert": "^0.2.3"

i want to use function of Soft assert 'softAssert(actual, expected, msg, ignoreKeys)'

But don't know, what is the exact steps to use it

Example: When i use soft assertion function in my code, getting following error.

If i use like this

  1. cy.softAssert(10, 12, "expected actual mismatch and will execute next line") : not supported or if i use different way like
  2. softAssert(10, 12, "expected actual mismatch and will execute next line") : SoftAssert not defined

Can any one tell me how to use this 'softAssert' function in cypress code with some small example.`


Now the problem I am facing

it('asserts and logs and fails', () => { 
  Cypress.softAssert(10, 12, "expected actual mismatch..."); 
  cy.log("text") 
  Cypress.softAssertAll(); 
}) 

I need my code after soft assertion as cy.log("text") to be executed in the same 'it' block but the current test failing the whole 'it' block, without executing 'cy.log("text")' statement.

1
Thanks for the reply. I have used it but some how wait and data retrieving from fixture file variable functionalities failed. Is there any other way, i can use it without adding anything in my index.jsSmith Ranjan

1 Answers

1
votes

The soft assertion concept is pretty cool, and you can add it with minimal implementation to Cypress

const jsonAssertion = require("soft-assert")

it('asserts several times and only fails at the end', () => {
  jsonAssertion.softAssert(10, 12, "expected actual mismatch");
  // some more assertions, not causing a failure

  jsonAssertion.softAssertAll();  // Now fail the test if above fails
})

To me, it would be nicer to see each soft assertion failure in the log, so it's possible to add custom commands to wrap the soft-assert functions

const jsonAssertion = require("soft-assert")

Cypress.Commands.add('softAssert', (actual, expected, message) => {
  jsonAssertion.softAssert(actual, expected, message)
  if (jsonAssertion.jsonDiffArray.length) {
    jsonAssertion.jsonDiffArray.forEach(diff => {

      const log = Cypress.log({
        name: 'Soft assertion error',
        displayName: 'softAssert',
        message: diff.error.message
      })
    
    })
  }
});
Cypress.Commands.add('softAssertAll', () => jsonAssertion.softAssertAll())


//-- all above can go into /cypress/support/index.js
//-- to save adding it to every test (runs once each test session)



it('asserts and logs but does not fail', () => {
  cy.softAssert(10, 12, "expected actual mismatch...");
  cy.log('text');    // this will run
})

it('asserts and logs and fails', () => {
  cy.softAssert(10, 12, "expected actual mismatch...");
  cy.log('text');    // this will run

  cy.softAssertAll();
})