Skip to content

Instantly share code, notes, and snippets.

@Flexicon
Last active May 2, 2024 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Flexicon/d32676365d30e7884872fcefbd1d1042 to your computer and use it in GitHub Desktop.
Save Flexicon/d32676365d30e7884872fcefbd1d1042 to your computer and use it in GitHub Desktop.
Showing how to handle thrown errors in Jest when testing asynchronous code
async function wontThrowErr() {
return 'ok';
}
async function mightThrowErr() {
throw new Error('oops');
}
module.exports = {
mightThrowErr,
wontThrowErr,
};
/* Based on: https://jestjs.io/docs/asynchronous#resolves--rejects */
const { mightThrowErr, wontThrowErr } = require('./async');
test('async error should be thrown', () => {
expect(mightThrowErr()).rejects.toThrow(Error('oops'));
});
test('async error should not be thrown', () => {
expect(wontThrowErr()).resolves.not.toThrow();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment