Skip to content

Commit

Permalink
Add an example of how to use async/await to the README
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 authored and domenic committed Sep 20, 2017
1 parent e237fd2 commit c3772a8
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ it("should change the state", function (done) {

Notice how `.notify(done)` is hanging directly off of `.should`, instead of appearing after a promise assertion. This indicates to Chai as Promised that it should pass fulfillment or rejection directly through to the testing framework. Thus, the above code will fail with a Chai as Promised error (`"expected promise to be fulfilled…"`) if `promise` is rejected, but will fail with a simple Chai error (`expected "before" to equal "after"`) if `otherState` does not change.

### Working with `async`/`await` and Promise-Friendly Test Runners

Since any assertion that must wait on a promise returns a promise itself, if you're able to use `async`/`await` and your test runner supports returning a promise from test methods, you can await assertions in tests. In many cases you can avoid using Chai as Promised at all by performing a synchronous assertion after an `await`, but awaiting `rejectedWith` is often more convenient than using `try`/`catch` blocks without Chai as Promised:

```javascript
it('should work well with async/await', async () => {
(await Promise.resolve(42)).should.equal(42)
await Promise.reject(new Error()).should.be.rejectedWith(Error);
});
```

### Multiple Promise Assertions

To perform assertions on multiple promises, use `Promise.all` to combine multiple Chai as Promised assertions:
Expand Down

0 comments on commit c3772a8

Please sign in to comment.