Commit f4f2a909 authored by Mark Florian's avatar Mark Florian Committed by Simon Knox

Document async test best practices

- Discourage done/done.fail callbacks in favour of async/await or
  promise chains
- Encourage expect(promise).rejects.toThrow() pattern for expected
  rejections. The previous example of a try/except violated the
  jest/no-try-expect ESLint rule!
parent 90b81bf9
...@@ -321,80 +321,56 @@ it('tests a promise', async () => { ...@@ -321,80 +321,56 @@ it('tests a promise', async () => {
}); });
it('tests a promise rejection', async () => { it('tests a promise rejection', async () => {
expect.assertions(1); await expect(user.getUserName(1)).rejects.toThrow('User with 1 not found.');
try {
await user.getUserName(1);
} catch (e) {
expect(e).toEqual({
error: 'User with 1 not found.',
});
}
}); });
``` ```
You can also work with Promise chains. In this case, you can make use of the `done` callback and `done.fail` in case an error occurred. Following are some examples: You can also simply return a promise from the test function.
NOTE: **Note:**
Using the `done` and `done.fail` callbacks is discouraged when working with
promises. They should only be used when testing callback-based code.
**Bad**: **Bad**:
```javascript ```javascript
// missing done callback // missing return
it('tests a promise', () => { it('tests a promise', () => {
promise.then(data => { promise.then(data => {
expect(data).toBe(asExpected); expect(data).toBe(asExpected);
}); });
}); });
// missing catch // uses done/done.fail
it('tests a promise', done => {
promise
.then(data => {
expect(data).toBe(asExpected);
})
.then(done);
});
// use done.fail in asynchronous tests
it('tests a promise', done => { it('tests a promise', done => {
promise promise
.then(data => { .then(data => {
expect(data).toBe(asExpected); expect(data).toBe(asExpected);
}) })
.then(done) .then(done)
.catch(fail); .catch(done.fail);
});
// missing catch
it('tests a promise rejection', done => {
promise
.catch(error => {
expect(error).toBe(expectedError);
})
.then(done);
}); });
``` ```
**Good**: **Good**:
```javascript ```javascript
// handling success // verifying a resolved promise
it('tests a promise', done => { it('tests a promise', () => {
promise return promise
.then(data => { .then(data => {
expect(data).toBe(asExpected); expect(data).toBe(asExpected);
}) });
.then(done)
.catch(done.fail);
}); });
// failure case // verifying a resolved promise using Jest's `resolves` matcher
it('tests a promise rejection', done => { it('tests a promise', () => {
promise return expect(promise).resolves.toBe(asExpected);
.then(done.fail) });
.catch(error => {
expect(error).toBe(expectedError); // verifying a rejected promise using Jest's `rejects` matcher
}) it('tests a promise rejection', () => {
.then(done) return expect(promise).rejects.toThrow(expectedError);
.catch(done.fail);
}); });
``` ```
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment