Files
jasmine/src/core/matchers/toThrowMatching.js
Steve Gravrock 1f23f1e4d2 Inject a per-runable pretty printer into MatchersUtil
This will allow us to add support for custom object formatters, which
will be a per-runable resource like custom matchers, by injecting them
into the pretty-printer.
2020-02-10 17:26:00 -08:00

70 lines
1.9 KiB
JavaScript

getJasmineRequireObj().toThrowMatching = function(j$) {
var usageError = j$.formatErrorMsg('<toThrowMatching>', 'expect(function() {<expectation>}).toThrowMatching(<Predicate>)');
/**
* {@link expect} a function to `throw` something matching a predicate.
* @function
* @name matchers#toThrowMatching
* @since 3.0.0
* @param {Function} predicate - A function that takes the thrown exception as its parameter and returns true if it matches.
* @example
* expect(function() { throw new Error('nope'); }).toThrowMatching(function(thrown) { return thrown.message === 'nope'; });
*/
function toThrowMatching(matchersUtil) {
return {
compare: function(actual, predicate) {
var thrown;
if (typeof actual !== 'function') {
throw new Error(usageError('Actual is not a Function'));
}
if (typeof predicate !== 'function') {
throw new Error(usageError('Predicate is not a Function'));
}
try {
actual();
return fail('Expected function to throw an exception.');
} catch (e) {
thrown = e;
}
if (predicate(thrown)) {
return pass('Expected function not to throw an exception matching a predicate.');
} else {
return fail(function() {
return 'Expected function to throw an exception matching a predicate, ' +
'but it threw ' + thrownDescription(thrown) + '.';
});
}
}
};
function thrownDescription(thrown) {
if (thrown && thrown.constructor) {
return j$.fnNameFor(thrown.constructor) + ' with message ' +
matchersUtil.pp(thrown.message);
} else {
return matchersUtil.pp(thrown);
}
}
}
function pass(message) {
return {
pass: true,
message: message
};
}
function fail(message) {
return {
pass: false,
message: message
};
}
return toThrowMatching;
};