August 2017
Beginner
374 pages
10h 41m
English
Since Jest Version 20, special matchers for promises are provided. Instead of returning a promise and using .then to check the result, you can use .resolves, as follows:
test('fetchPosts returns array of posts', () => { expect.assertions(1) return expect(fetchPosts()).resolves.toContain({ title: 'test' })})
The same also works for failing promises, via .rejects:
test('logging in without username/password should fail', () => { expect.assertions(1) return expect(login()).rejects.toMatch('could not login')})
Read now
Unlock full access