Created
November 12, 2018 23:09
-
-
Save douglasnaphas/619b37db42404b3c133f642271fa4a42 to your computer and use it in GitHub Desktop.
Examples of testing with Jest and Promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test('promise series', () => { | |
new Promise(resolve => { | |
resolve('a'); | |
}).then(Promise.resolve()); | |
}); | |
const fetchBeverageList = () => | |
new Promise((resolve, reject) => { | |
resolve(4); | |
}); | |
test('resolving promise', () => | |
new Promise((resolve, reject) => { | |
resolve(); // change to reject();, and it will fail | |
})); | |
test('4 is 4', () => { | |
fetchBeverageList().then(four => expect(four).toBe(4)); | |
}); | |
const fivePromise = () => new Promise(a => a(5)); | |
const failingFivePromise = () => new Promise(reject => reject(5)); | |
test('expect 5', () => fivePromise().then(five => expect(five).toBe(5))); | |
test('expect 5, fail', () => | |
failingFivePromise().catch(five => expect(five).toBe(5))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment