when rejected

  • <Promise> when rejected <assertion>

Wait for a promise to be rejected, then delegate the reason to another assertion.

var rejectedPromise = new Promise(function (resolve, reject) {
    setTimeout(function () {
        reject(new Error('argh'));
    }, 1);
});
 
return expect(rejectedPromise, 'when rejected', 'to equal', new Error('argh'));

It works with any assertion or expect.it construct:

return expect(Promise.reject(new Error('argh')), 'when rejected', expect.it('to have message', 'argh'));

If the response is fulfilled, the assertion fails with the following output:

return expect(Promise.resolve(123), 'when rejected', 'to have message', 'argh');
expected Promise when rejected to have message 'argh'
  
Promise unexpectedly fulfilled with 123