dzm
dzm

Reputation: 23534

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

Upvotes: 3

Views: 1210

Answers (1)

dzm
dzm

Reputation: 23534

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

Upvotes: 2

Related Questions