Package Exports
- catch-and-match
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (catch-and-match) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
catch-and-match
A simple utility for testing and inspecting an error thrown by a function.
Made with ❤ at @outlandish
Install
npm install catch-and-match --save-devUsage
catchAndMatch(fn, matcher[, cb])
fn {Function} function that should throw
matcher {RegExp|String|Function} method of inspecting error:
- a Function is passed the Error
- a String is turned to simple RegExp (
new RegExp(str)) - a RegExp is tested against the Error message (
re.test(err.message))
cb {Function} error-first callback indicating success of catch and match
Example Usage
function log (str) {
if (typeof str !== 'string') {
throw new Error('str should be a string');
}
console.log(str);
}
// Passes with string matcher
it('should throw an error without correct arguments', function (cb) {
catchAndMatch(log.bind(undefined, 'hello'), /should be a string/, cb);
// or, using Promises:
// return catchAndMatch(log.bind(undefined, 'hello'));
});
// Fails with RegExp matcher
it('should throw an error without correct arguments', function (cb) {
catchAndMatch(log.bind(undefined, 10), /should be a string/, cb);
// catchAndMatch(log.bind(undefined, 10), cb);
});
// Passes with custom inspection
it('should throw an error without correct arguments', function (cb) {
return catchAndMatch(log.bind(undefined, 10), function (err) {
return err.indexOf('should be a string') !== -1;
});
});