Search code examples
mocha.jschaiassertions

How to run the test case even if assertion fails using chaijs


I'm just curious to know how to run the test cases even of the assertion fails within the test script using mocha & chai js.

var arr = ["2017-05-04","04-05-2017","05-2017-04"];
function isDate(date) {
var regEx = /^\d{4}-\d{2}-\d{2}$/;
return date.match(regEx) != null;
}
for(var i=0;i<arr.length;i++){
 assert.strictEqual(isDate(arr[i]), true);
}

i want the test case to be executed even if any of the assertion fails.


Solution

  • When an assertion fails inside a test, an exception is thrown, and there's no way to continue execution of the same test after the exception has been thrown.

    What you can do in your case is make each of your test handle one case from your array. This way, each case is handled as a separate test and the failure of one test has no impact on the other tests. For instance:

    var chai = require("chai");
    
    var assert = chai.assert;
    
    var arr = ["2017-05-04","04-05-2017","05-2017-04"];
    function isDate(date) {
        var regEx = /^\d{4}-\d{2}-\d{2}$/;
        return date.match(regEx) != null;
    }
    
    describe("date tests", function () {
        function makeTest(testCase) {
            it(testCase, function () {
                assert.strictEqual(isDate(testCase), true);
            });
        }
    
        for(var i=0;i<arr.length;i++){
            makeTest(arr[i]);
        }
    });