3

I have two backend projects P1 & P2. Data from P1 has to flow into P2 after some processing via a middleware. I am writing this middleware and I have to create an E2E testing module.

I will have 100s of test cases and in each there may be 3 or 4 expect statements. The chai 'expect' function is a form of hard assertion. How can I get soft assertions in javascript. Basically, the test case will run all 3 or 4 expect statements and report which one's failed.

1

2 Answers 2

3

Chai does not allow soft asserts, it is against their assertion philosophy. Try using the library https://www.npmjs.com/package/soft-assert

Sign up to request clarification or add additional context in comments.

4 Comments

If that is true then Chai is the wrong assertion library to use. It forces Automation QA to create 1 for 1 assertions for tests meaning longer execution times and inefficient design. If tests do have multiple assertions, then the metrics will be inconsistent depending on if the first assertion fails (All assertions failed) and the last assertion failed (All but one assertion passed). While popular Chai clearly has no QA SDETs on their team with real world profession level experience.
@DarkArtsWizard that is not true. 1 for 1 tests to assertions is not forced, you could just use Hard Assertions and have the test fail as soon as it hits the first assertion failure rather than accumulate the failed assertions. As mentioned above its an assertion philosophy. What philosophy you adhere to is up to you and your team.
Hard Asserts as a philosophy is completely ridiculous. You might (and should) have 30 - 40 validations in your smoke tests. And maybe 100-200 in your end to end test. Try telling your client the happy path results that took a mere 2 minutes to complete at the Sprint Demo now will take hours to get results. Because all the Date formats changed breaking your first assertion? And due to Hard Assertion you don't know if it effects the one, ten or all assertions? You will a garner terrible reputation for that level of metrics reporting. Soft Asserts. Day One. No Brainer.
@DarkArtsWizard not sure why you are arguing the point with me. I am stating their philosophy not promoting it.
2

We needed something similar and the library proposed by Raymond was not enough for us (we didn't want to change the assertion library and also the library lacks a lot of assertion types we needed), so I wrote this one that I think perfectly answers the question: https://github.com/alfonso-presa/soft-assert

With this soft-assert library you can wrap other assetion libraries (like chai expect that you asked for) so that you can perform both soft and hard assertions in your tests. Here you have an example:

const { proxy, flush } = require("@alfonso-presa/soft-assert");
const { expect } = require("chai");
const softExpect = proxy(expect);

describe("something", () => {
    it("should capture exceptions with wrapped chai expectation library", () => {
        softExpect("a").to.equal("b");
        softExpect(false).to.be.true;
        softExpect(() => {}).to.throw("Error");
        softExpect(() => {throw new Error();}).to.not.throw();
        try {
            //This is just to showcase, you should not try catch the result of flush.
            flush();
            //As there are assertion errors above this will not be reached
            expect(false).toBeTruthy();
        } catch(e) {
            expect(e.message).toContain("expected 'a' to equal 'b'");
            expect(e.message).toContain("expected false to be true");
            expect(e.message).toContain("to throw an error");
            expect(e.message).toContain("to not throw an error but 'Error' was thrown");
        }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.