Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chai expect to throw with error message

I'm trying to test the message of an Error using expect().to.throw. I read that expect needs to be passed a function to execute, as oppose to the function that returns. Anyways, I still can't seem to get this to work, I've tried a number of variations, but this is the basic setup in jsfiddle. Any ideas why this won't work or how can I fix it?

Thank you

like image 252
dzm Avatar asked Feb 15 '26 05:02

dzm


1 Answers

The issue was that I was using throw to check, but I wasn't actually throwing an error, but returning an Error. The following evaluates correctly:

expect(test.throwFunction()).to.be.an.instanceOf(Error).with.property('message', 'This is an error message')

However, I'm not sure this is the cleanest way to check - seems like there could be a shorthand version.

Update: Looks like there's a work in progress for adding to.be.an.error

like image 179
dzm Avatar answered Feb 17 '26 19:02

dzm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!