Created
March 29, 2016 19:36
-
-
Save eschwartz/2313f0cd71c57ae5f56bc4da8d297df3 to your computer and use it in GitHub Desktop.
Assertion that a promise-fn fails
This file contains hidden or 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
const assert = require('assert'); | |
const _ = require('lodash'); | |
function throwsAsync(block, expectedError, message) { | |
return block() | |
.then(res => { | |
assert.fail(res, expectedError, message, 'is'); | |
}, err => { | |
if (_.isFunction(expectedError)) { | |
// note that assert.throws doesn't work with custom Error constructors | |
//https://github.com/nodejs/node/issues/3188 | |
return assert(err instanceof expectedError, `Expected error {${err.name}} to be instanceof ${expectedError.name}`); | |
} | |
assert.throws(() => { throw err; }, expectedError, message); | |
}); | |
} | |
module.exports = throwsAsync; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment