Using Mocha, I am attempting to test whether a constructor throws an error. I haven't been able to do this using the expect syntax, so I'd like to do the following:
it('should throw exception when instantiated', function() {
try {
new ErrorThrowingObject();
// Force the test to fail since error wasn't thrown
}
catch (error) {
// Constructor threw Error, so test succeeded.
}
}
Is this possible?
With Chai
throw
(ES2016)http://chaijs.com/api/bdd/#method_throw
For clarity... This works
This doesn't work
MarkJ's accepted answer is the way to go and way simpler than others here. Let me show example in real world:
should.js
Using the should.js library with should.fail
Alternative you can use the should throwError
Chai
And with chai using the throw api