unexpected
Version:
Extensible BDD assertion toolkit
65 lines (48 loc) • 1.67 kB
Markdown
Asserts that a promise is fulfilled.
```javascript
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:
```javascript
var rejectedPromise = expect.promise(function (resolve, reject) {
setTimeout(function () {
reject(new Error('argh'));
}, 1);
});
return expect(rejectedPromise, 'to be fulfilled');
```
```output
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:
```javascript
var promiseThatWillBeFulfilledWithAValue = expect.promise(function (resolve, reject) {
setTimeout(function () {
resolve('abc');
}, 1);
});
```
```javascript
return expect(promiseThatWillBeFulfilledWithAValue, 'to be fulfilled with', 'abc');
```
The expected value will be matched against the value with
[](/assertions/any/to-satisfy/) semantics, so you can also pass a string,
a regular expression, a function, or an object:
```javascript
return expect(promiseThatWillBeFulfilledWithAValue, 'to be fulfilled with', /b/);
```
You get a nice diff if the assertion fails:
```javascript
return expect(promiseThatWillBeFulfilledWithAValue, 'to be fulfilled with', 'def');
```
```output
expected Promise (fulfilled) => 'abc' to be fulfilled with 'def'
expected 'abc' to equal 'def'
-abc
+def
```