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

52 lines
1.6 KiB
JavaScript

getJasmineRequireObj().toBeRejectedWith = function(j$) {
/**
* Expect a promise to be rejected with a value equal to the expected, using deep equality comparison.
* @function
* @async
* @name async-matchers#toBeRejectedWith
* @since 3.3.0
* @param {Object} expected - Value that the promise is expected to be rejected with
* @example
* await expectAsync(aPromise).toBeRejectedWith({prop: 'value'});
* @example
* return expectAsync(aPromise).toBeRejectedWith({prop: 'value'});
*/
return function toBeRejectedWith(matchersUtil) {
return {
compare: function(actualPromise, expectedValue) {
if (!j$.isPromiseLike(actualPromise)) {
throw new Error('Expected toBeRejectedWith to be called on a promise.');
}
function prefix(passed) {
return 'Expected a promise ' +
(passed ? 'not ' : '') +
'to be rejected with ' + matchersUtil.pp(expectedValue);
}
return actualPromise.then(
function() {
return {
pass: false,
message: prefix(false) + ' but it was resolved.'
};
},
function(actualValue) {
if (matchersUtil.equals(actualValue, expectedValue)) {
return {
pass: true,
message: prefix(true) + '.'
};
} else {
return {
pass: false,
message: prefix(false) + ' but it was rejected with ' + matchersUtil.pp(actualValue) + '.'
};
}
}
);
}
};
};
};