Skip to content

Instantly share code, notes, and snippets.

@douglasnaphas
Created November 12, 2018 23:09
Show Gist options
  • Save douglasnaphas/619b37db42404b3c133f642271fa4a42 to your computer and use it in GitHub Desktop.
Save douglasnaphas/619b37db42404b3c133f642271fa4a42 to your computer and use it in GitHub Desktop.
Examples of testing with Jest and Promises
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