I'm trying to catch an error which should be throw by the function getUserRecommendations
. This is my example:
it('should throw an error when no user ID is provided', (done) => {
expect(async () => {
await Client.getUserRecommendations(null, {})
}).to.throw(/Missing/)
})
Unfortunately it doesn't work and I get as result that my test it doesn't pass along with this message:
AssertionError: expected [Function] to throw an error
The way you have set up the the test won't work because expect.to.throw
is not expecting a promise. At least I think that is what is going on based on this issue.
The best alternative is to use chai-as-promised
and do something like:
it('should throw an error when no user ID is provided', () => {
expect(Client.getUserRecommendations(null, {})).be.rejectedWith(/Missing/);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With