8

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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Smith Ranjan
  • 97
  • 2
  • 6
  • You can check this out https://stackoverflow.com/questions/55868107/does-cypress-support-soft-assertion/55879165#55879165 – Alapan Das Feb 10 '21 at 07:18
  • 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.js – Smith Ranjan Feb 10 '21 at 07:53

1 Answers1

11

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();
})
Richard Matsen
  • 20,671
  • 3
  • 43
  • 77
  • Hi Richard, Thanks for the info Now the problem i am facing it('asserts and logs and fails', () => { Cypress.softAssert(10, 12, "expected actual mismatch and will execute next line"); 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 faling the whole 'it' block, whithout executing 'cy.log("text")' statement. – Smith Ranjan Feb 10 '21 at 09:26
  • My eyes are spinning, so I added that to the question. – Richard Matsen Feb 10 '21 at 09:35
  • I see the problem, the `Cypress.softAssert()` function is synchronous in the test, but commands like `cy.log()` are queued and asynchronous (so the test ends before they can execute). – Richard Matsen Feb 10 '21 at 09:42
  • Also, the log message from `Cypress.softAssertAll()` seems too much. I'll see if it can be done better. – Richard Matsen Feb 10 '21 at 09:43
  • It works better as a custom command, small changes made to the code above (`cy` not `Cypress`). – Richard Matsen Feb 10 '21 at 09:52
  • Great it works, Thanks. Now only problem is the assertion error is so lengthy and if we can get some one liner it will be great. – Smith Ranjan Feb 10 '21 at 10:09
  • I can check that tomorrow, but you might figure it out - take a look at properties of `jsonAssertion` inside `Cypress.Commands.add('softAssertAll', () => `, something there may be useful to `cy.log()`. – Richard Matsen Feb 10 '21 at 10:52
  • Hello, I can't add these soft assertion Cypress commands to my project. I use cypress 10.4 my project has index.js, e2e.js, and commands.js Is there any more detailed example about how to implement soft assertion code into cypress version +10? – Barış Can Ateş Sep 29 '22 at 10:21