Explore Node's assert
module documentation specifically focused on the assert.ifError
function.
When used,
assert.ifError
will throw an error if the provided value is not undefined or null. This comes in handy during testing, especially when examining the error argument within callbacks. The stack trace will display all frames corresponding to the error passed to ifError(), along with any new frames introduced by ifError() itself.
assert.ifError(null);
// OK
assert.ifError(0);
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0
assert.ifError('error');
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'
assert.ifError(new Error());
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error
Curious about Jest's equivalent of asser.IfError
? Let's dive into it!