简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM