Wednesday, 15 March 2017

Catching specific errors in Chai

I'm using the "Chai" package to test my code, but I'm having trouble with catching errors.

I'll have a block like follows:

describe("My function", function() {
    it("Should throw an error when I give it bad data", function() {
        expect(() => { myFunction(someData); }.to.throw(Error);
    });
});

But when I run the test I get:

 Error: expected [Function] to not throw 'Error' but 'ClientError: Date must be of type Object' was thrown

In this case the "bad data" had a date that was intentionally messed up, but you can see that the generic "Error" doesn't match the thrown "ClientError", even though an error was "caught". So how do I catch a specific, planned error?

Note that this is not the usual problem of "I can't catch a thrown error", I am catching errors just fine, I am just not passing tests because the errors expected vs caught is different.



via mstorkson

No comments:

Post a Comment