to be fulfilled

  • <Promise> to be fulfilled

Asserts that a promise is fulfilled.

var promiseThatWillBeFulfilled = expect.promise(function (resolve, reject) {
    setTimeout(resolve, 1);
});
 
return expect(promiseThatWillBeFulfilled, 'to be fulfilled');

If the promise is rejected, the assertion will fail with the following output:

var rejectedPromise = expect.promise(function (resolve, reject) {
    setTimeout(function () {
        reject(new Error('argh'));
    }, 1);
});
 
return expect(rejectedPromise, 'to be fulfilled');
expected Promise (rejected) => Error('argh'to be fulfilled
  
Promise (rejected) => Error('argh') unexpectedly rejected with Error('argh')

You can assert the promise is fulfilled with a specific value by passing a second parameter:

var promiseThatWillBeFulfilledWithAValue = expect.promise(function (resolve, reject) {
    setTimeout(function () {
        resolve('abc');
    }, 1);
});
return expect(promiseThatWillBeFulfilledWithAValue, 'to be fulfilled with', 'abc');

The expected value will be matched against the value with to satisfy semantics, so you can also pass a string, a regular expression, a function, or an object:

return expect(promiseThatWillBeFulfilledWithAValue, 'to be fulfilled with', /b/);

You get a nice diff if the assertion fails:

return expect(promiseThatWillBeFulfilledWithAValue, 'to be fulfilled with', 'def');
expected Promise (fulfilled) => 'abc' to be fulfilled with 'def'
  
expected 'abc' to equal 'def'
 
-
abc
+
def