0

I have implemented the answer from this Does cypress support soft assertion? successfully and can softAssert(expression, 'message') like so. I won't repost the code below, it is included in the link.

However, I have noticed that when using the mochawesome-report generator it's report content uses the body from

'test:after:run', (result) => {
    result.body
}

Usually in Cypress this is given by the window.it where .it represents each it('test title') block in the spec file.

But when the softAssert is used, specifically in this part, where window.it is overwritten from commands.js:

// monkey-patch `it` callback so we insert `cy.then()` as a last command 
// to each test case where we'll assert if there are any soft assertion errors
function itCallback ( func ) {
    func();
    cy.then(() => {
        if ( errors.length ) {
            const _ = Cypress._;
            let msg = '';

            if ( Cypress.browser.isHeaded ) {

                msg = 'Failed soft assertions... check log above ↑';
            } else {

                _.each( errors, error => {
                    msg += '\n' + error;
                });

                msg = msg.replace(/^/gm, '\t');
            }

            throw new Error(msg);
        }
    });
}

const origIt = window.it;
window.it = (title, func) => {
    origIt(title, func && (() => itCallback(func)));
};
window.it.only = (title, func) => {
    origIt.only(title, func && (() => itCallback(func)));
};
window.it.skip = (title, func) => {
    origIt.skip(title, func);
};

The issue is that my report is now showing the test body as:

return itCallback(func)

And I assume that it is because of this thing: origIt(title, func && (() => itCallback(func)));

How do I fix this and return the actual it() block body while keeping the softAssert functionality.

I have tried for the past few days to solve this and am having no luck. If https://stackoverflow.com/users/927631/dwelle or https://stackoverflow.com/users/5878476/jennifer-shehane or https://stackoverflow.com/users/4168257/gleb-bahmutov are online, I need a real pro to reach out, I'm stumped.

1 Answers1

1

I guess they call it monkey-patch for a reason.

It looks like you are trying to run the error collection code after the it(), why not use afterEach()?

afterEach(() => {
  if ( errors.length ) {
    const separator = '\n\t';
    const msg = Cypress.browser.isHeaded ? 
      'Failed soft assertions... check log above ↑' :
      seperator + errors.join(separator);
    throw msg;
  }
})

A better way to do soft assertions is given here How can i use soft assertion in Cypress.

It makes use of the soft-assert package (17000 downloads pw) and gives you a couple of simple custom commands to implement

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())