From 9fef780881a2be69ad59d87f4c5bc9cb08a89f5f Mon Sep 17 00:00:00 2001 From: Carl-Erik Kopseng Date: Sat, 4 Nov 2023 12:11:17 +0100 Subject: [PATCH] Make stub.throws be able to throw strings refs #1679 --- lib/sinon/default-behaviors.js | 6 ++---- test/proxy-call-test.js | 6 ++++-- test/stub-test.js | 37 ++++++++++++---------------------- 3 files changed, 19 insertions(+), 30 deletions(-) diff --git a/lib/sinon/default-behaviors.js b/lib/sinon/default-behaviors.js index f15d13646..716b7c04c 100644 --- a/lib/sinon/default-behaviors.js +++ b/lib/sinon/default-behaviors.js @@ -13,11 +13,9 @@ const useRightMostCallback = -2; function throwsException(fake, error, message) { if (typeof error === "function") { fake.exceptionCreator = error; - } else if (typeof error === "string") { + } else if (typeof error === "string" && typeof message !== "undefined") { fake.exceptionCreator = function () { - const newException = new Error( - message || `Sinon-provided ${error}`, - ); + const newException = new Error(message); newException.name = error; return newException; }; diff --git a/test/proxy-call-test.js b/test/proxy-call-test.js index 1cc210293..6faa9424d 100644 --- a/test/proxy-call-test.js +++ b/test/proxy-call-test.js @@ -1126,7 +1126,9 @@ describe("sinonSpy.call", function () { }); it("includes exception", function () { - const object = { doIt: sinonStub().throws("TypeError") }; + const object = { + doIt: sinonStub().throws("TypeError", "some message"), + }; assert.exception(function () { object.doIt(); @@ -1134,7 +1136,7 @@ describe("sinonSpy.call", function () { assert.equals( object.doIt.getCall(0).toString().replace(/ at.*/g, ""), - "doIt() !TypeError(Sinon-provided TypeError)", + "doIt() !TypeError(some message)", ); }); diff --git a/test/stub-test.js b/test/stub-test.js index c8f7c113d..20c042a78 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -986,14 +986,15 @@ describe("stub", function () { assert.same(stub.throws({}), stub); }); - it("sets type of exception to throw", function () { + it("can throw a string", function () { const stub = createStub(); - const exceptionType = "TypeError"; - stub.throws(exceptionType); + stub.throws("a reason"); - assert.exception(function () { + try { stub(); - }, exceptionType); + } catch (reason) { + assert.equals(reason, "a reason"); + } }); it("specifies exception message", function () { @@ -1006,15 +1007,6 @@ describe("stub", function () { }); }); - it("does not specify exception message if not provided", function () { - const stub = createStub(); - stub.throws("Error"); - - assert.exception(stub, { - message: "", - }); - }); - it("throws generic error", function () { const stub = createStub(); stub.throws(); @@ -1036,19 +1028,16 @@ describe("stub", function () { assert.contains(stub.firstCall.toString(), "not implemented"); }); - it("creates a non empty error message when error is a string and no message is passed", function () { + it("throws a string when the error is a string and no message is passed", function () { const stub = createStub(); - stub.withArgs(1).throws("TypeError"); + stub.withArgs(1).throws("something happened"); - assert.exception( - function () { - stub(1); - }, - { - message: "Sinon-provided TypeError", - }, - ); + try { + stub(1); + } catch (err) { + assert.equals(err, "something happened"); + } }); describe("lazy instantiation of exceptions", function () {