From e3f0389ac2914d69f3e153a549523a2c589dd371 Mon Sep 17 00:00:00 2001 From: Sheel Choksi Date: Thu, 5 Sep 2013 23:02:22 -0700 Subject: [PATCH] Change andThrow to always throw an Error If an error is passed in, it is thrown, otherwise the argument passed in is wrapped in an Error [finishes #50607615][closes #372] --- lib/jasmine-core/jasmine.js | 3 ++- spec/core/SpyStrategySpec.js | 12 +++++++++++- src/core/SpyStrategy.js | 3 ++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 1bcd067d..240ae3f9 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -1565,8 +1565,9 @@ getJasmineRequireObj().SpyStrategy = function() { }; this.callThrow = function(something) { + var error = (something instanceof Error) ? something : new Error(something); plan = function() { - throw something; + throw error; }; return getSpy(); }; diff --git a/spec/core/SpyStrategySpec.js b/spec/core/SpyStrategySpec.js index fb162d6c..496c8b73 100644 --- a/spec/core/SpyStrategySpec.js +++ b/spec/core/SpyStrategySpec.js @@ -50,9 +50,19 @@ describe("SpyStrategy", function() { var originalFn = jasmine.createSpy("original"), spyStrategy = new j$.SpyStrategy({fn: originalFn}); + spyStrategy.callThrow(new TypeError("bar")); + + expect(function() { spyStrategy.exec(); }).toThrowError(TypeError, "bar"); + expect(originalFn).not.toHaveBeenCalled(); + }); + + it("allows a non-Error to be thrown, wrapping it into an exception when executed", function() { + var originalFn = jasmine.createSpy("original"), + spyStrategy = new j$.SpyStrategy({fn: originalFn}); + spyStrategy.callThrow("bar"); - expect(function() { spyStrategy.exec(); }).toThrow("bar"); + expect(function() { spyStrategy.exec(); }).toThrowError(Error, "bar"); expect(originalFn).not.toHaveBeenCalled(); }); diff --git a/src/core/SpyStrategy.js b/src/core/SpyStrategy.js index 8a4a869e..fc6bb9fc 100644 --- a/src/core/SpyStrategy.js +++ b/src/core/SpyStrategy.js @@ -29,8 +29,9 @@ getJasmineRequireObj().SpyStrategy = function() { }; this.callThrow = function(something) { + var error = (something instanceof Error) ? something : new Error(something); plan = function() { - throw something; + throw error; }; return getSpy(); };