Skip to content

Instantly share code, notes, and snippets.

@irokhes
Created November 28, 2018 14:51
Show Gist options
  • Save irokhes/7e4c1cdd989c4304478331ac3fdad904 to your computer and use it in GitHub Desktop.
Save irokhes/7e4c1cdd989c4304478331ac3fdad904 to your computer and use it in GitHub Desktop.
Function to assert against promise rejection
//Credit to:
//https://www.nearform.com/blog/avoiding-common-hurdles-in-unit-testing/
const expectRejection = async (p, message) => {
try {
await p
} catch (error) {
if (error.message === message) return
throw new Error('Expected error "${message}" but found "${error.message}"')
}
throw new Error('Expected error "${message}" but no error occurred.')
}
test('invalid input causes an error', async () => {
await expectRejection(doSomeTest({ input: 'invalid' }), 'Validation failed.')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment