Last active
May 2, 2024 15:47
-
-
Save Flexicon/d32676365d30e7884872fcefbd1d1042 to your computer and use it in GitHub Desktop.
Showing how to handle thrown errors in Jest when testing asynchronous code
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
async function wontThrowErr() { | |
return 'ok'; | |
} | |
async function mightThrowErr() { | |
throw new Error('oops'); | |
} | |
module.exports = { | |
mightThrowErr, | |
wontThrowErr, | |
}; |
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
/* 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