Created
July 5, 2018 23:29
-
-
Save lionello/c7575d84aea4fc0d8536f373ff744651 to your computer and use it in GitHub Desktop.
Assertion helper for async/promise methods that should fail
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
Assert.rejects = async function rejects (blockOrPromise, error, message) { | |
return new Promise((resolve,reject) => { | |
if (typeof blockOrPromise === 'function') { | |
blockOrPromise = blockOrPromise() | |
} | |
blockOrPromise.then(x => { | |
reject(Error('Should fail, not return ' + x)) | |
}).catch(async err => { | |
if (typeof error === 'function') { | |
err = (await error(err)) || err | |
} | |
else if (error instanceof RegExp) { | |
Assert.ok(error.test(err.message), message || err.stack) | |
} | |
else if (typeof error === 'string') { | |
Assert.strictEqual(err.message, error, message || err.stack) | |
} | |
else { | |
Assert.equal(err, error, message || err.stack) | |
} | |
resolve(err) | |
}) | |
.catch(reject) | |
}) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment