I am writing a test suite that tests promise-returning functions. One common theme of these tests is the need to check if a promise-returning functions properly throws an error when passed invalid arguments. I have tried using should.throws, but in looking through the code I see it is not designed to work with promises.
I have made the following utility function to get the functionality I need:
var TestUtil = module.exports;
var should = require('should');
/**
* Checks if a promise chain throws an error, and optionally that the error includes
* the given errorMsg
* @param promise {String} (optional) error message to check for
* @param errorMsg
*/
TestUtil.throws = function(promise, errorMsg) {
return promise
.then(function(res) {
throw new Error(); // should never reach this point
})
.catch(function(e) {
if (errorMsg) {
e.message.should.include(errorMsg);
}
should.exist(e);
});
};
Does there exist a shouldjs function that does this same thing? I would like to keep my tests cohesive by only using the shouldjs api for checking, rather than using this one-off function.