Skip to content

Instantly share code, notes, and snippets.

@eschwartz
Created March 29, 2016 19:36
Show Gist options
  • Save eschwartz/2313f0cd71c57ae5f56bc4da8d297df3 to your computer and use it in GitHub Desktop.
Save eschwartz/2313f0cd71c57ae5f56bc4da8d297df3 to your computer and use it in GitHub Desktop.
Assertion that a promise-fn fails
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