diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 8a035ef6..e47313ab 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -2107,7 +2107,7 @@ getJasmineRequireObj().toThrowError = function() { } if (errorType && message) { - if (util.equals(thrown, new errorType(message))) { + if (thrown.constructor == errorType && util.equals(thrown.message, message)) { return pass("Expected function not to throw Error with message \"" + message + "\"."); } else { return fail("Expected function to throw Error with message \"" + message + "\"."); @@ -2172,7 +2172,7 @@ getJasmineRequireObj().toThrowError = function() { regexp = expected; } else if (typeof expected == "string") { message = expected; - } else if (typeof expected == "function" && new expected() instanceof Error) { + } else if (checkForAnErrorType(expected)) { errorType = expected; } @@ -2180,7 +2180,7 @@ getJasmineRequireObj().toThrowError = function() { throw new Error("Expected is not an Error, string, or RegExp."); } } else { - if (typeof arguments[1] == "function" && new arguments[1]() instanceof Error) { + if (checkForAnErrorType(arguments[1])) { errorType = arguments[1]; } else { throw new Error("Expected error type is not an Error."); @@ -2195,6 +2195,16 @@ getJasmineRequireObj().toThrowError = function() { } } } + + function checkForAnErrorType(type) { + if (typeof type !== "function") { + return false; + } + + var Surrogate = function() {}; + Surrogate.prototype = type.prototype; + return (new Surrogate()) instanceof Error; + } } }; } diff --git a/spec/core/matchers/toThrowErrorSpec.js b/spec/core/matchers/toThrowErrorSpec.js index 818df958..dda3df97 100644 --- a/spec/core/matchers/toThrowErrorSpec.js +++ b/spec/core/matchers/toThrowErrorSpec.js @@ -25,7 +25,7 @@ describe("toThrowError", function() { }; expect(function() { - matcher.compare(fn, "string", "foo"); + matcher.compare(fn, void 0, "foo"); }).toThrow(new Error("Expected error type is not an Error.")); // TODO: this needs to change for self-test }); diff --git a/src/core/matchers/toThrowError.js b/src/core/matchers/toThrowError.js index 74835575..212fe22e 100644 --- a/src/core/matchers/toThrowError.js +++ b/src/core/matchers/toThrowError.js @@ -124,14 +124,14 @@ getJasmineRequireObj().toThrowError = function() { } function checkForAnErrorType(type) { - if (typeof expected == "function") { + if (typeof type !== "function") { return false; } var Surrogate = function() {}; Surrogate.prototype = type.prototype; return (new Surrogate()) instanceof Error; - }; + } } }; }