Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.