Merge branch 'toBeRejectedWith' of https://github.com/codymikol/jasmine into codymikol-toBeRejectedWith

This commit is contained in:
Gregg Van Hove
2018-10-22 11:15:05 -07:00
2 changed files with 153 additions and 1 deletions

View File

@@ -23,7 +23,7 @@ getJasmineRequireObj().AsyncExpectation = function(j$) {
throw new Error('Expected expectAsync to be called with a promise.');
}
['toBeResolved', 'toBeRejected', 'toBeResolvedTo'].forEach(wrapCompare.bind(this));
['toBeResolved', 'toBeRejected', 'toBeResolvedTo', 'toBeRejectedTo'].forEach(wrapCompare.bind(this));
}
function wrapCompare(name) {
@@ -137,6 +137,49 @@ getJasmineRequireObj().AsyncExpectation = function(j$) {
);
};
/**
* Expect a promise to be rejected to a value equal to the expected, using deep equality comparison.
* @function
* @async
* @name async-matchers#toBeRejectedTo
* @param {Object} expected - Value that the promise is expected to reject to
* @example
* await expectAsync(aPromise).toBeRejectedTo({prop: 'value'});
* @example
* return expectAsync(aPromise).toBeRejectedTo({prop: 'value'});
*/
AsyncExpectation.prototype.toBeRejectedTo = function(actualPromise, expectedValue) {
var self = this;
function prefix(passed) {
return 'Expected a promise ' +
(passed ? 'not ' : '') +
'to be rejected to ' + j$.pp(expectedValue);
}
return actualPromise.then(
function() {
return {
pass: false,
message: prefix(false) + ' but it was resolved.'
};
},
function(actualValue) {
if (self.util.equals(actualValue, expectedValue, self.customEqualityTesters)) {
return {
pass: true,
message: prefix(true) + '.'
};
} else {
return {
pass: false,
message: prefix(false) + ' but it was rejected to ' + j$.pp(actualValue) + '.'
};
}
}
);
};
AsyncExpectation.factory = function(options) {
var expect = new AsyncExpectation(options);