Fix failure messages for toBeRejectedWithError matcher

This commit is contained in:
Alexey Prokhorov
2019-05-03 09:37:11 +04:00
parent 35968e4a60
commit 8ab46566ac
2 changed files with 56 additions and 15 deletions

View File

@@ -6,7 +6,10 @@ describe('#toBeRejectedWithError', function () {
actual = Promise.reject(new TypeError('foo'));
return matcher.compare(actual, TypeError).then(function (result) {
expect(result).toEqual(jasmine.objectContaining({ pass: true }));
expect(result).toEqual(jasmine.objectContaining({
pass: true,
message: 'Expected a promise not to be rejected with TypeError, but it was.'
}));
});
});
@@ -17,7 +20,10 @@ describe('#toBeRejectedWithError', function () {
actual = Promise.reject(new TypeError('foo'));
return matcher.compare(actual, TypeError, 'foo').then(function (result) {
expect(result).toEqual(jasmine.objectContaining({ pass: true }));
expect(result).toEqual(jasmine.objectContaining({
pass: true,
message: 'Expected a promise not to be rejected with TypeError: \'foo\', but it was.'
}));
});
});
@@ -28,7 +34,10 @@ describe('#toBeRejectedWithError', function () {
actual = Promise.reject(new Error('foo'));
return matcher.compare(actual, 'foo').then(function (result) {
expect(result).toEqual(jasmine.objectContaining({ pass: true }));
expect(result).toEqual(jasmine.objectContaining({
pass: true,
message: 'Expected a promise not to be rejected with Error: \'foo\', but it was.'
}));
});
});
@@ -39,7 +48,10 @@ describe('#toBeRejectedWithError', function () {
actual = Promise.reject(new Error('foo'));
return matcher.compare(actual, /foo/).then(function (result) {
expect(result).toEqual(jasmine.objectContaining({ pass: true }));
expect(result).toEqual(jasmine.objectContaining({
pass: true,
message: 'Expected a promise not to be rejected with Error: /foo/, but it was.'
}));
});
});
@@ -50,7 +62,24 @@ describe('#toBeRejectedWithError', function () {
actual = Promise.reject(new Error());
return matcher.compare(actual, '').then(function (result) {
expect(result).toEqual(jasmine.objectContaining({ pass: true }));
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 matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(jasmineUnderTest.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.'
}));
});
});
@@ -61,7 +90,10 @@ describe('#toBeRejectedWithError', function () {
actual = Promise.resolve(new Error('foo'));
return matcher.compare(actual, 'foo').then(function (result) {
expect(result).toEqual(jasmine.objectContaining({ pass: false }));
expect(result).toEqual(jasmine.objectContaining({
pass: false,
message: 'Expected a promise to be rejected but it was resolved.'
}));
});
});
@@ -72,7 +104,10 @@ describe('#toBeRejectedWithError', function () {
actual = Promise.reject('foo');
return matcher.compare(actual, 'foo').then(function (result) {
expect(result).toEqual(jasmine.objectContaining({ pass: false }));
expect(result).toEqual(jasmine.objectContaining({
pass: false,
message: 'Expected a promise to be rejected with Error: \'foo\' but it was rejected with \'foo\'.'
}));
});
});
@@ -83,7 +118,10 @@ describe('#toBeRejectedWithError', function () {
actual = Promise.reject(new Error('foo'));
return matcher.compare(actual, TypeError, 'foo').then(function (result) {
expect(result).toEqual(jasmine.objectContaining({ pass: false }));
expect(result).toEqual(jasmine.objectContaining({
pass: false,
message: 'Expected a promise to be rejected with TypeError: \'foo\' but it was rejected with type Error.'
}));
});
});
@@ -94,7 +132,10 @@ describe('#toBeRejectedWithError', function () {
actual = Promise.reject(new Error('foo'));
return matcher.compare(actual, 'bar').then(function (result) {
expect(result).toEqual(jasmine.objectContaining({ pass: false }));
expect(result).toEqual(jasmine.objectContaining({
pass: false,
message: 'Expected a promise to be rejected with Error: \'bar\' but it was rejected with Error: foo.'
}));
});
});
});

View File

@@ -33,11 +33,11 @@ getJasmineRequireObj().toBeRejectedWithError = function(j$) {
function matchError(actual, expected) {
if (!j$.isError_(actual)) {
return fail(expected, 'not rejected with Error.');
return fail(expected, 'rejected with ' + j$.pp(actual));
}
if (!(actual instanceof expected.error)) {
return fail(expected, 'rejected with type ' + j$.fnNameFor(actual.constructor) + '.');
return fail(expected, 'rejected with type ' + j$.fnNameFor(actual.constructor));
}
var actualMessage = actual.message;
@@ -50,20 +50,20 @@ getJasmineRequireObj().toBeRejectedWithError = function(j$) {
return pass(expected);
}
return fail(expected, 'rejected with ' + j$.pp(actual) + '.');
return fail(expected, 'rejected with ' + j$.pp(actual));
}
function pass(expected) {
return {
pass: true,
message: 'Expected a promise to be rejected with ' + expected.printValue + '.'
message: 'Expected a promise not to be rejected with ' + expected.printValue + ', but it was.'
};
}
function fail(expected, message) {
return {
pass: false,
message: 'Expected a promise to be rejected with ' + expected.printValue + ' but it was ' + message
message: 'Expected a promise to be rejected with ' + expected.printValue + ' but it was ' + message + '.'
};
}
@@ -82,7 +82,7 @@ getJasmineRequireObj().toBeRejectedWithError = function(j$) {
return {
error: error,
message: message,
printValue: j$.fnNameFor(error) + ': ' + j$.pp(message)
printValue: j$.fnNameFor(error) + (typeof message === 'undefined' ? '' : ': ' + j$.pp(message))
};
}
};