From c339605834f6fd7ba3afdd179fff3a8544e62bd7 Mon Sep 17 00:00:00 2001 From: Mark de Dios <99303358+peanutenthusiast@users.noreply.github.com> Date: Wed, 13 Sep 2023 09:16:14 -0700 Subject: [PATCH] fix(2525): ensure non empty message when error of type string is passed, but no message (#2544) Co-authored-by: Morgan Roderick <20321+mroderick@users.noreply.github.com> --- lib/sinon/default-behaviors.js | 4 +++- test/proxy-call-test.js | 2 +- test/stub-test.js | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/sinon/default-behaviors.js b/lib/sinon/default-behaviors.js index 6f1fcadd7..1be5f9af1 100644 --- a/lib/sinon/default-behaviors.js +++ b/lib/sinon/default-behaviors.js @@ -15,7 +15,9 @@ function throwsException(fake, error, message) { fake.exceptionCreator = error; } else if (typeof error === "string") { fake.exceptionCreator = function () { - const newException = new Error(message || ""); + const newException = new Error( + message || `Sinon-provided ${error}` + ); newException.name = error; return newException; }; diff --git a/test/proxy-call-test.js b/test/proxy-call-test.js index 8cbec51ca..524d58f7f 100644 --- a/test/proxy-call-test.js +++ b/test/proxy-call-test.js @@ -1134,7 +1134,7 @@ describe("sinonSpy.call", function () { assert.equals( object.doIt.getCall(0).toString().replace(/ at.*/g, ""), - "doIt() !TypeError" + "doIt() !TypeError(Sinon-provided TypeError)" ); }); diff --git a/test/stub-test.js b/test/stub-test.js index 33dac0ef4..977a04120 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -940,6 +940,21 @@ 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 () { + const stub = createStub(); + + stub.withArgs(1).throws("TypeError"); + + assert.exception( + function () { + stub(1); + }, + { + message: "Sinon-provided TypeError", + } + ); + }); + describe("lazy instantiation of exceptions", function () { let errorSpy; beforeEach(function () {