Skip to content

Instantly share code, notes, and snippets.

@MauricioRobayo
Last active August 15, 2022 18:26
Show Gist options
  • Select an option

  • Save MauricioRobayo/ac82411721eca226eac70879d344b392 to your computer and use it in GitHub Desktop.

Select an option

Save MauricioRobayo/ac82411721eca226eac70879d344b392 to your computer and use it in GitHub Desktop.
Testing Asynchronous Code
test('the data is peanut butter', async () => {
const data = await fetchData();
expect(data).toBe('peanut butter');
});
test('the fetch fails with an error', async () => {
expect.assertions(1);
try {
await fetchData();
} catch (e) {
expect(e).toMatch('error');
}
});
test('the data is peanut butter', async () => {
await expect(fetchData()).resolves.toBe('peanut butter');
});
test('the fetch fails with an error', async () => {
await expect(fetchData()).rejects.toMatch('error');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment