This makes it easier to write high quality matchers and asymmetric equality
testers, and is also a step toward supporting custom object formatters.
Previously, Jasmine passed custom object formatters as the second argument
to matcher factories and as and the second argument to asymmetric equality
testers' `asymmetricMatch` method. Matchers and asymmetric equality testers
were responsible for passing the custom object formatters to methods like
`matchersUtil#equals`:
function toEqual(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
// ...
result.pass = util.equals(actual, expected, customEqualityTesters, diffBuilder);
And:
ArrayContaining.prototype.asymmetricMatch = function(other, customTesters) {
// ...
for (var i = 0; i < this.sample.length; i++) {
var item = this.sample[i];
if (!j$.matchersUtil.contains(other, item, customTesters)) {
return false;
}
}
With this change, that is no longer necessary. Matchers and asymmetric
equality testers can ignore the existence of custom equality testers and
still fully support them:
function toEqual(util) {
return {
compare: function(actual, expected) {
// ...
result.pass = util.equals(actual, expected, diffBuilder);
And:
ArrayContaining.prototype.asymmetricMatch = function(other, matchersUtil) {
// ...
for (var i = 0; i < this.sample.length; i++) {
var item = this.sample[i];
if (!matchersUtil.contains(other, item)) {
return false;
}
}
The old interfaces are still supported, for now, but will be deprecated
in a future commit and removed in the next major release after that.
In addition to making matchers and custom equality testers simpler,
this change sets the stage for adding support for custom object
formatters. Those will be architecturally similar to custom equality
testers, and by injecting a `MatchersUtil` instance everywhere we can
add them without requiring user code to pass them around as used to be
the case with custom object formatters.
182 lines
6.4 KiB
JavaScript
182 lines
6.4 KiB
JavaScript
describe('#toBeRejectedWithError', function () {
|
|
it('passes when Error type matches', function () {
|
|
jasmine.getEnv().requirePromises();
|
|
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
|
|
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(matchersUtil),
|
|
actual = Promise.reject(new TypeError('foo'));
|
|
|
|
return matcher.compare(actual, TypeError).then(function (result) {
|
|
expect(result).toEqual(jasmine.objectContaining({
|
|
pass: true,
|
|
message: 'Expected a promise not to be rejected with TypeError, but it was.'
|
|
}));
|
|
});
|
|
});
|
|
|
|
it('passes when Error type and message matches', function () {
|
|
jasmine.getEnv().requirePromises();
|
|
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
|
|
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(matchersUtil),
|
|
actual = Promise.reject(new TypeError('foo'));
|
|
|
|
return matcher.compare(actual, TypeError, 'foo').then(function (result) {
|
|
expect(result).toEqual(jasmine.objectContaining({
|
|
pass: true,
|
|
message: 'Expected a promise not to be rejected with TypeError: \'foo\', but it was.'
|
|
}));
|
|
});
|
|
});
|
|
|
|
it('passes when Error matches and is exactly Error', function() {
|
|
jasmine.getEnv().requirePromises();
|
|
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
|
|
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(matchersUtil),
|
|
actual = Promise.reject(new Error());
|
|
|
|
return matcher.compare(actual, Error).then(function (result) {
|
|
expect(result).toEqual(jasmine.objectContaining({
|
|
pass: true,
|
|
message: 'Expected a promise not to be rejected with Error, but it was.'
|
|
}));
|
|
});
|
|
|
|
});
|
|
|
|
it('passes when Error message matches a string', function () {
|
|
jasmine.getEnv().requirePromises();
|
|
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
|
|
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(matchersUtil),
|
|
actual = Promise.reject(new Error('foo'));
|
|
|
|
return matcher.compare(actual, 'foo').then(function (result) {
|
|
expect(result).toEqual(jasmine.objectContaining({
|
|
pass: true,
|
|
message: 'Expected a promise not to be rejected with Error: \'foo\', but it was.'
|
|
}));
|
|
});
|
|
});
|
|
|
|
it('passes when Error message matches a RegExp', function () {
|
|
jasmine.getEnv().requirePromises();
|
|
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
|
|
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(matchersUtil),
|
|
actual = Promise.reject(new Error('foo'));
|
|
|
|
return matcher.compare(actual, /foo/).then(function (result) {
|
|
expect(result).toEqual(jasmine.objectContaining({
|
|
pass: true,
|
|
message: 'Expected a promise not to be rejected with Error: /foo/, but it was.'
|
|
}));
|
|
});
|
|
});
|
|
|
|
it('passes when Error message is empty', function () {
|
|
jasmine.getEnv().requirePromises();
|
|
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
|
|
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(matchersUtil),
|
|
actual = Promise.reject(new Error());
|
|
|
|
return matcher.compare(actual, '').then(function (result) {
|
|
expect(result).toEqual(jasmine.objectContaining({
|
|
pass: true,
|
|
message: 'Expected a promise not to be rejected with Error: \'\', but it was.'
|
|
}));
|
|
});
|
|
});
|
|
|
|
it('passes when no arguments', function () {
|
|
jasmine.getEnv().requirePromises();
|
|
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
|
|
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(matchersUtil),
|
|
actual = Promise.reject(new Error());
|
|
|
|
return matcher.compare(actual, void 0).then(function (result) {
|
|
expect(result).toEqual(jasmine.objectContaining({
|
|
pass: true,
|
|
message: 'Expected a promise not to be rejected with Error, but it was.'
|
|
}));
|
|
});
|
|
});
|
|
|
|
it('fails when resolved', function () {
|
|
jasmine.getEnv().requirePromises();
|
|
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
|
|
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(matchersUtil),
|
|
actual = Promise.resolve(new Error('foo'));
|
|
|
|
return matcher.compare(actual, 'foo').then(function (result) {
|
|
expect(result).toEqual(jasmine.objectContaining({
|
|
pass: false,
|
|
message: 'Expected a promise to be rejected but it was resolved.'
|
|
}));
|
|
});
|
|
});
|
|
|
|
it('fails when rejected with non Error type', function () {
|
|
jasmine.getEnv().requirePromises();
|
|
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
|
|
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(matchersUtil),
|
|
actual = Promise.reject('foo');
|
|
|
|
return matcher.compare(actual, 'foo').then(function (result) {
|
|
expect(result).toEqual(jasmine.objectContaining({
|
|
pass: false,
|
|
message: 'Expected a promise to be rejected with Error: \'foo\' but it was rejected with \'foo\'.'
|
|
}));
|
|
});
|
|
});
|
|
|
|
it('fails when Error type mismatches', function () {
|
|
jasmine.getEnv().requirePromises();
|
|
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
|
|
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(matchersUtil),
|
|
actual = Promise.reject(new Error('foo'));
|
|
|
|
return matcher.compare(actual, TypeError, 'foo').then(function (result) {
|
|
expect(result).toEqual(jasmine.objectContaining({
|
|
pass: false,
|
|
message: 'Expected a promise to be rejected with TypeError: \'foo\' but it was rejected with type Error.'
|
|
}));
|
|
});
|
|
});
|
|
|
|
it('fails when Error message mismatches', function () {
|
|
jasmine.getEnv().requirePromises();
|
|
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
|
|
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(matchersUtil),
|
|
actual = Promise.reject(new Error('foo'));
|
|
|
|
return matcher.compare(actual, 'bar').then(function (result) {
|
|
expect(result).toEqual(jasmine.objectContaining({
|
|
pass: false,
|
|
message: 'Expected a promise to be rejected with Error: \'bar\' but it was rejected with Error: foo.'
|
|
}));
|
|
});
|
|
});
|
|
|
|
it('fails if actual is not a promise', function() {
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
|
|
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(matchersUtil),
|
|
actual = 'not a promise';
|
|
|
|
function f() {
|
|
return matcher.compare(actual);
|
|
}
|
|
|
|
expect(f).toThrowError(
|
|
'Expected toBeRejectedWithError to be called on a promise.'
|
|
);
|
|
});
|
|
});
|