Detailed error messages in toThrow/toThrowError

- included what was thrown for failure messages in toThrow and toThrowError
- fixed typo from 'execption' to 'exception' in toThrowError failure messages
- clarified failure messages in toThrowError to include specific error types

[Fixes #52680709]
This commit is contained in:
Sheel Choksi
2013-07-12 23:21:10 -07:00
parent 5b986c953c
commit c91df21a96
5 changed files with 57 additions and 49 deletions

View File

@@ -2073,7 +2073,7 @@ getJasmineRequireObj().toThrow = function(j$) {
if (arguments.length == 1) {
result.pass = true;
result.message = "Expected function not to throw.";
result.message = "Expected function not to throw, but it threw " + j$.pp(thrown) + ".";
return result;
}
@@ -2082,7 +2082,7 @@ getJasmineRequireObj().toThrow = function(j$) {
result.pass = true;
result.message = "Expected function not to throw " + j$.pp(expected) + ".";
} else {
result.message = "Expected function to throw " + j$.pp(expected) + ".";
result.message = "Expected function to throw " + j$.pp(expected) + ", but it threw " + j$.pp(thrown) + ".";
}
return result;
@@ -2130,17 +2130,19 @@ getJasmineRequireObj().toThrowError = function(j$) {
if (errorType && message) {
if (thrown.constructor == errorType && util.equals(thrown.message, message)) {
return pass("Expected function not to throw Error with message \"" + message + "\".");
return pass("Expected function not to throw " + errorType.name + " with message \"" + message + "\".");
} else {
return fail("Expected function to throw Error with message \"" + message + "\".");
return fail("Expected function to throw " + errorType.name + " with message \"" + message +
"\", but it threw " + thrown.constructor.name + " with message \"" + thrown.message + "\".");
}
}
if (errorType && regexp) {
if (thrown.constructor == errorType && regexp.test(thrown.message)) {
return pass("Expected function not to throw Error with message matching " + regexp + ".");
return pass("Expected function not to throw " + errorType.name + " with message matching " + regexp + ".");
} else {
return fail("Expected function to throw Error with message matching " + regexp + ".");
return fail("Expected function to throw " + errorType.name + " with message matching " + regexp +
", but it threw " + thrown.constructor.name + " with message \"" + thrown.message + "\".");
}
}
@@ -2148,23 +2150,25 @@ getJasmineRequireObj().toThrowError = function(j$) {
if (thrown.constructor == errorType) {
return pass("Expected function not to throw " + errorType.name + ".");
} else {
return fail("Expected function to throw " + errorType.name + ".");
return fail("Expected function to throw " + errorType.name + ", but it threw " + thrown.constructor.name + ".");
}
}
if (message) {
if (thrown.message == message) {
return pass("Expected function not to throw an execption with message " + j$.pp(message) + ".");
return pass("Expected function not to throw an exception with message " + j$.pp(message) + ".");
} else {
return fail("Expected function to throw an execption with message " + j$.pp(message) + ".");
return fail("Expected function to throw an exception with message " + j$.pp(message) +
", but it threw an exception with message " + j$.pp(thrown.message) + ".");
}
}
if (regexp) {
if (regexp.test(thrown.message)) {
return pass("Expected function not to throw an execption with a message matching " + j$.pp(regexp) + ".");
return pass("Expected function not to throw an exception with a message matching " + j$.pp(regexp) + ".");
} else {
return fail("Expected function to throw an execption with a message matching " + j$.pp(regexp) + ".");
return fail("Expected function to throw an exception with a message matching " + j$.pp(regexp) +
", but it threw an exception with message " + j$.pp(thrown.message) + ".");
}
}

View File

@@ -77,7 +77,7 @@ describe("toThrowError", function() {
expect(result.message).toEqual("Expected function to throw an Error, but it threw undefined.");
});
it("passes if thrown is an Error, but there is no expected error", function() {
it("passes if thrown is a type of Error, but there is no expected error", function() {
var matcher = j$.matchers.toThrowError(),
fn = function() {
throw new TypeError();
@@ -100,7 +100,7 @@ describe("toThrowError", function() {
result = matcher.compare(fn, "foo");
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an execption with message 'foo'.");
expect(result.message).toEqual("Expected function not to throw an exception with message 'foo'.");
});
it("fails if thrown is an Error and the expected is not the same message", function() {
@@ -113,7 +113,7 @@ describe("toThrowError", function() {
result = matcher.compare(fn, "bar");
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an execption with message 'bar'.");
expect(result.message).toEqual("Expected function to throw an exception with message 'bar', but it threw an exception with message 'foo'.");
});
it("passes if thrown is an Error and the expected is a RegExp that matches the message", function() {
@@ -126,7 +126,7 @@ describe("toThrowError", function() {
result = matcher.compare(fn, /long/);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an execption with a message matching /long/.");
expect(result.message).toEqual("Expected function not to throw an exception with a message matching /long/.");
});
it("fails if thrown is an Error and the expected is a RegExp that does not match the message", function() {
@@ -139,7 +139,7 @@ describe("toThrowError", function() {
result = matcher.compare(fn, /foo/);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an execption with a message matching /foo/.");
expect(result.message).toEqual("Expected function to throw an exception with a message matching /foo/, but it threw an exception with message 'a long message'.");
});
it("passes if thrown is an Error and the expected the same Error", function() {
@@ -191,23 +191,23 @@ describe("toThrowError", function() {
result = matcher.compare(fn, TypeError);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw TypeError.");
expect(result.message).toEqual("Expected function to throw TypeError, but it threw Error.");
});
it("passes if thrown is an Error and it is equal to the expected Error and message", function() {
it("passes if thrown is a type of Error and it is equal to the expected Error and message", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').andReturn(true)
},
matcher = j$.matchers.toThrowError(util),
fn = function() {
throw new Error("foo");
throw new TypeError("foo");
},
result;
result = matcher.compare(fn, Error, "foo");
result = matcher.compare(fn, TypeError, "foo");
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw Error with message \"foo\".");
expect(result.message).toEqual("Expected function not to throw TypeError with message \"foo\".");
});
it("passes if thrown is a custom error that takes arguments and it is equal to the expected custom error and message", function() {
@@ -227,54 +227,54 @@ describe("toThrowError", function() {
result = matcher.compare(fn, CustomError, "foo");
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw Error with message \"foo\".");
expect(result.message).toEqual("Expected function not to throw CustomError with message \"foo\".");
});
it("fails if thrown is an Error and the expected is a different Error", function() {
it("fails if thrown is a type of Error and the expected is a different Error", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').andReturn(false)
},
matcher = j$.matchers.toThrowError(util),
fn = function() {
throw new Error("foo");
throw new TypeError("foo");
},
result;
result = matcher.compare(fn, Error, "bar");
result = matcher.compare(fn, TypeError, "bar");
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw Error with message \"bar\".");
expect(result.message).toEqual("Expected function to throw TypeError with message \"bar\", but it threw TypeError with message \"foo\".");
});
it("passes if thrown is an Error and has the same type as the expected Error and the message matches the exepcted message", function() {
it("passes if thrown is a type of Error and has the same type as the expected Error and the message matches the exepcted message", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').andReturn(true)
},
matcher = j$.matchers.toThrowError(util),
fn = function() {
throw new Error("foo");
throw new TypeError("foo");
},
result;
result = matcher.compare(fn, Error, /foo/);
result = matcher.compare(fn, TypeError, /foo/);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw Error with message matching /foo/.");
expect(result.message).toEqual("Expected function not to throw TypeError with message matching /foo/.");
});
it("fails if thrown is an Error and the expected is a different Error", function() {
it("fails if thrown is a type of Error and the expected is a different Error", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').andReturn(false)
},
matcher = j$.matchers.toThrowError(util),
fn = function() {
throw new Error("foo");
throw new TypeError("foo");
},
result;
result = matcher.compare(fn, Error, /bar/);
result = matcher.compare(fn, TypeError, /bar/);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw Error with message matching /bar/.");
expect(result.message).toEqual("Expected function to throw TypeError with message matching /bar/, but it threw TypeError with message \"foo\".");
});
});

View File

@@ -33,7 +33,7 @@ describe("toThrow", function() {
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw.");
expect(result.message).toEqual("Expected function not to throw, but it threw 5.");
});
it("passes even if what is thrown is falsy", function() {
@@ -45,7 +45,7 @@ describe("toThrow", function() {
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw.");
expect(result.message).toEqual("Expected function not to throw, but it threw undefined.");
});
it("passes if what is thrown is equivalent to what is expected", function() {
@@ -77,7 +77,7 @@ describe("toThrow", function() {
result = matcher.compare(fn, "foo");
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw 'foo'.");
expect(result.message).toEqual("Expected function to throw 'foo', but it threw 5.");
});
it("fails if what is thrown is not equivalent to undefined", function() {
@@ -93,6 +93,6 @@ describe("toThrow", function() {
result = matcher.compare(fn, void 0);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw undefined.");
expect(result.message).toEqual("Expected function to throw undefined, but it threw 5.");
});
});

View File

@@ -25,7 +25,7 @@ getJasmineRequireObj().toThrow = function(j$) {
if (arguments.length == 1) {
result.pass = true;
result.message = "Expected function not to throw.";
result.message = "Expected function not to throw, but it threw " + j$.pp(thrown) + ".";
return result;
}
@@ -34,7 +34,7 @@ getJasmineRequireObj().toThrow = function(j$) {
result.pass = true;
result.message = "Expected function not to throw " + j$.pp(expected) + ".";
} else {
result.message = "Expected function to throw " + j$.pp(expected) + ".";
result.message = "Expected function to throw " + j$.pp(expected) + ", but it threw " + j$.pp(thrown) + ".";
}
return result;

View File

@@ -35,17 +35,19 @@ getJasmineRequireObj().toThrowError = function(j$) {
if (errorType && message) {
if (thrown.constructor == errorType && util.equals(thrown.message, message)) {
return pass("Expected function not to throw Error with message \"" + message + "\".");
return pass("Expected function not to throw " + errorType.name + " with message \"" + message + "\".");
} else {
return fail("Expected function to throw Error with message \"" + message + "\".");
return fail("Expected function to throw " + errorType.name + " with message \"" + message +
"\", but it threw " + thrown.constructor.name + " with message \"" + thrown.message + "\".");
}
}
if (errorType && regexp) {
if (thrown.constructor == errorType && regexp.test(thrown.message)) {
return pass("Expected function not to throw Error with message matching " + regexp + ".");
return pass("Expected function not to throw " + errorType.name + " with message matching " + regexp + ".");
} else {
return fail("Expected function to throw Error with message matching " + regexp + ".");
return fail("Expected function to throw " + errorType.name + " with message matching " + regexp +
", but it threw " + thrown.constructor.name + " with message \"" + thrown.message + "\".");
}
}
@@ -53,23 +55,25 @@ getJasmineRequireObj().toThrowError = function(j$) {
if (thrown.constructor == errorType) {
return pass("Expected function not to throw " + errorType.name + ".");
} else {
return fail("Expected function to throw " + errorType.name + ".");
return fail("Expected function to throw " + errorType.name + ", but it threw " + thrown.constructor.name + ".");
}
}
if (message) {
if (thrown.message == message) {
return pass("Expected function not to throw an execption with message " + j$.pp(message) + ".");
return pass("Expected function not to throw an exception with message " + j$.pp(message) + ".");
} else {
return fail("Expected function to throw an execption with message " + j$.pp(message) + ".");
return fail("Expected function to throw an exception with message " + j$.pp(message) +
", but it threw an exception with message " + j$.pp(thrown.message) + ".");
}
}
if (regexp) {
if (regexp.test(thrown.message)) {
return pass("Expected function not to throw an execption with a message matching " + j$.pp(regexp) + ".");
return pass("Expected function not to throw an exception with a message matching " + j$.pp(regexp) + ".");
} else {
return fail("Expected function to throw an execption with a message matching " + j$.pp(regexp) + ".");
return fail("Expected function to throw an exception with a message matching " + j$.pp(regexp) +
", but it threw an exception with message " + j$.pp(thrown.message) + ".");
}
}