Skip to content

Commit

Permalink
Add back docs for the old way of testing promises (jestjs#3256)
Browse files Browse the repository at this point in the history
* Add back docs for the old way of testing promises

Fixes jestjs#3250

* Add `return` note to old version
  • Loading branch information
rogeliog authored and skovhus committed Apr 29, 2017
1 parent d4a059d commit d2c7a55
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions docs/TestingAsyncCode.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,24 @@ If `done()` is never called, the test will fail, which is what you want to happe

### Promises

##### available in Jest **20.0.0+**

If your code uses promises, there is a simpler way to handle asynchronous tests. Just use the `resolves` keyword in your expect statement, and Jest will wait for that promise to resolve. If the promise is rejected, the test will automatically fail.
If your code uses promises, there is a simpler way to handle asynchronous tests. Just return a promise from your test, and Jest will wait for that promise to resolve. If the promise is rejected, the test will automatically fail.

For example, let's say that `fetchData`, instead of using a callback, returns a promise that is supposed to resolve to the string `"peanut butter"`. We could test it with:

```js
test('the data is peanut butter', () => {
return fetchData().then(data => {
expect(data).toBe('peanut butter');
});
});
```

Be sure to return the promise - if you omit this `return` statement, your test will complete before `fetchData` completes.

##### available in Jest **20.0.0+**

You can also use the `resolves` keyword in your expect statement, and Jest will wait for that promise to resolve. If the promise is rejected, the test will automatically fail.

```js
test('the data is peanut butter', () => {
return expect(fetchData()).resolves.toBe('peanut butter');
Expand Down

0 comments on commit d2c7a55

Please sign in to comment.