Merge branch 'codymikol-toBeRejectedWith'

- Merges #1615 from @codymikol
- Closes #1600
- Fixes #1595
This commit is contained in:
Gregg Van Hove
2018-10-22 11:19:11 -07:00
3 changed files with 200 additions and 5 deletions

View File

@@ -2168,7 +2168,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', 'toBeRejectedWith'].forEach(wrapCompare.bind(this));
}
function wrapCompare(name) {
@@ -2282,6 +2282,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#toBeRejectedWith
* @param {Object} expected - Value that the promise is expected to reject to
* @example
* await expectAsync(aPromise).toBeRejectedWith({prop: 'value'});
* @example
* return expectAsync(aPromise).toBeRejectedWith({prop: 'value'});
*/
AsyncExpectation.prototype.toBeRejectedWith = 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);