Files
jasmine/src/core/matchers/toThrow.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

59 lines
1.7 KiB
JavaScript

getJasmineRequireObj().toThrow = function(j$) {
var getErrorMsg = j$.formatErrorMsg('<toThrow>', 'expect(function() {<expectation>}).toThrow()');
/**
* {@link expect} a function to `throw` something.
* @function
* @name matchers#toThrow
* @since 2.0.0
* @param {Object} [expected] - Value that should be thrown. If not provided, simply the fact that something was thrown will be checked.
* @example
* expect(function() { return 'things'; }).toThrow('foo');
* expect(function() { return 'stuff'; }).toThrow();
*/
function toThrow(matchersUtil) {
return {
compare: function(actual, expected) {
var result = { pass: false },
threw = false,
thrown;
if (typeof actual != 'function') {
throw new Error(getErrorMsg('Actual is not a Function'));
}
try {
actual();
} catch (e) {
threw = true;
thrown = e;
}
if (!threw) {
result.message = 'Expected function to throw an exception.';
return result;
}
if (arguments.length == 1) {
result.pass = true;
result.message = function() { return 'Expected function not to throw, but it threw ' + matchersUtil.pp(thrown) + '.'; };
return result;
}
if (matchersUtil.equals(thrown, expected)) {
result.pass = true;
result.message = function() { return 'Expected function not to throw ' + matchersUtil.pp(expected) + '.'; };
} else {
result.message = function() { return 'Expected function to throw ' + matchersUtil.pp(expected) + ', but it threw ' + matchersUtil.pp(thrown) + '.'; };
}
return result;
}
};
}
return toThrow;
};