Using expect(...).to.throw(...) with custom assertion function
See original GitHub issueI would like to be able to run more detailed assertions against a thrown exception than what currently seems possible. It would be awesome if expect(...).to.throw(...)
supported something like the following pattern:
expect(function() {
someFunctionThatThrows();
}).to.throw(function(err) {
expect(err.toString()).to.equal('Error: something is wrong here');
expect(err.actual).to.not.exist;
expect(err.expected).to.not.exist;
});
I currently have this implemented in a local helper, but I think that supporting this might make sense in chai
itself:
Assertion.overwriteMethod('throw', function (_super) {
return function(fn) {
if (typeof fn === 'function') {
var obj = this._obj;
try {
obj();
} catch (err) {
return fn(err);
}
throw new chai.AssertionError('expected [Function] to throw an exception');
} else {
_super.apply(this, arguments);
}
};
});
Note that this doesn’t support using it with .not
and I’m not sure if that would actually make sense. That case should probably be handled though and fail if the 'negate'
flag is encountered.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:12 (8 by maintainers)
Top Results From Across the Web
Expect a function to throw an exception in Jest - eloquent code
Suppose we want to test the following function using Node.js and assert that it indeed throws an error: func.js:
Read more >Mocha / Chai expect.to.throw not catching thrown errors
You have to pass a function to expect . Like this: expect(model.get.bind(model, 'z')).to.throw('Property does not exist in model schema.
Read more >Expect - Jest
Instead, you will use expect along with a "matcher" function to assert something about a value. It's easier to understand this with an...
Read more >Expect / Should - Chai Assertion Library
Both use the same chainable language to construct assertions, but they differ in the way an assertion ... expect(function () {}).to.not.throw(); expect({a: ...
Read more >assert.throws() - QUnit API
When testing code that is expected to throw an exception based on a specific set of ... description here' ); // using a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Turbo87 There’s a few solutions here, let me go through them:
If you want to assert on all of the properties for equality,
.deep.equal
will work:If you want to assert on some of the properties for equality,
.members
will work:However, if you want to assert some semantics over individual properties, we don’t have a way to do that just yet. You could use
satisfy
for now:Alternatively, you can subscribe to issue https://github.com/chaijs/chai/issues/644 - which is working out how to implement a matcher API for this kind of thing.
Hopefully that gives you some ideas to work with @Turbo87. Let us know if that’s helpful, or if you want to discuss things further 😄
Ok, this error message was very confusing. For anyone else who might be having the same issue the solution was this: