Asserts that a promise is rejected.
var promiseThatWillBeRejected = expect.promise(function (resolve, reject) {
setTimeout(reject, 1);
});
return expect(promiseThatWillBeRejected, 'to be rejected');
If the promise is fulfilled, the assertion will fail with the following output:
var fulfilledPromise = expect.promise(function (resolve, reject) {
setTimeout(resolve, 1);
});
return expect(fulfilledPromise, 'to be rejected');
expected Promise (fulfilled) to be rejected
Promise (fulfilled) unexpectedly fulfilled
You can assert the promise is rejected with a specific reason (error) by passing a second parameter:
var promiseThatWillBeRejectedWithAReason = expect.promise(function (resolve, reject) {
setTimeout(function () {
reject(new Error('Oh dear'));
}, 1);
});
return expect(promiseThatWillBeRejectedWithAReason, 'to be rejected with', new Error('Oh dear'));
The expected reason will be matched against the rejection reason with to satisfy semantics, so you can also pass a string, a regular expression, a function, or an object:
return expect(promiseThatWillBeRejectedWithAReason, 'to be rejected with', /dear/);
You get a nice diff if the assertion fails:
return expect(promiseThatWillBeRejectedWithAReason, 'to be rejected with', new Error('bugger'));
expected Promise (rejected) => Error('Oh dear') to be rejected with Error('bugger')
expected Error('Oh dear') to satisfy Error('bugger')
Error({
message:
'Oh dear'
//
//
//
should equal
'bugger'
-
Oh dear
+
bugger
})