From 2a83dfed01fab1a1d1de3dea1c1a99feff555b4c Mon Sep 17 00:00:00 2001 From: Mark de Dios Date: Mon, 11 Sep 2023 16:44:05 -0700 Subject: [PATCH 1/5] Add test case for when throwsException is passed an error of type string but no message --- lib/sinon/default-behaviors.js | 2 +- test/stub-test.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/sinon/default-behaviors.js b/lib/sinon/default-behaviors.js index 6f1fcadd7..9c373de13 100644 --- a/lib/sinon/default-behaviors.js +++ b/lib/sinon/default-behaviors.js @@ -15,7 +15,7 @@ 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 || error || ""); newException.name = error; return newException; }; diff --git a/test/stub-test.js b/test/stub-test.js index 33dac0ef4..1a7331407 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -940,6 +940,18 @@ 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("apple pie") + + assert.exception(function () { + stub(1) + }, { + message: "apple pie" + }) + }) + describe("lazy instantiation of exceptions", function () { let errorSpy; beforeEach(function () { From c89e05ea25faf817554cb8a954321eda735883cd Mon Sep 17 00:00:00 2001 From: Mark de Dios Date: Tue, 12 Sep 2023 10:37:43 -0700 Subject: [PATCH 2/5] Modify behavior for creating error message from error of type string to indicate sinon created the error --- lib/sinon/default-behaviors.js | 2 +- test/proxy-call-test.js | 2 +- test/stub-test.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/sinon/default-behaviors.js b/lib/sinon/default-behaviors.js index 9c373de13..fc82f132b 100644 --- a/lib/sinon/default-behaviors.js +++ b/lib/sinon/default-behaviors.js @@ -15,7 +15,7 @@ function throwsException(fake, error, message) { fake.exceptionCreator = error; } else if (typeof error === "string") { fake.exceptionCreator = function () { - const newException = new Error(message || error || ""); + 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 1a7331407..f92bd5769 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -948,7 +948,7 @@ describe("stub", function () { assert.exception(function () { stub(1) }, { - message: "apple pie" + message: "Sinon-provided apple pie" }) }) From e677cb44bd06b4e321d665788c7450e925869551 Mon Sep 17 00:00:00 2001 From: Mark de Dios Date: Tue, 12 Sep 2023 17:41:00 -0700 Subject: [PATCH 3/5] Update test case to emulate potential errors --- test/stub-test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/stub-test.js b/test/stub-test.js index f92bd5769..a966849d2 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -943,12 +943,12 @@ describe("stub", function () { 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("apple pie") + stub.withArgs(1).throws("TypeError") assert.exception(function () { stub(1) }, { - message: "Sinon-provided apple pie" + message: "Sinon-provided TypeError" }) }) From f7021aee50e40928e41f59f1f42c92233062b9fa Mon Sep 17 00:00:00 2001 From: Mark de Dios Date: Wed, 13 Sep 2023 05:54:15 -0700 Subject: [PATCH 4/5] Apply prettier --- lib/sinon/default-behaviors.js | 4 +++- test/stub-test.js | 19 +++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/sinon/default-behaviors.js b/lib/sinon/default-behaviors.js index fc82f132b..901e2fb65 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 || `Sinon-provided ${error}` || ""); + const newException = new Error( + message || `Sinon-provided ${error}` || "" + ); newException.name = error; return newException; }; diff --git a/test/stub-test.js b/test/stub-test.js index a966849d2..977a04120 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -941,16 +941,19 @@ describe("stub", function () { }); it("creates a non empty error message when error is a string and no message is passed", function () { - const stub = createStub() + const stub = createStub(); - stub.withArgs(1).throws("TypeError") + stub.withArgs(1).throws("TypeError"); - assert.exception(function () { - stub(1) - }, { - message: "Sinon-provided TypeError" - }) - }) + assert.exception( + function () { + stub(1); + }, + { + message: "Sinon-provided TypeError", + } + ); + }); describe("lazy instantiation of exceptions", function () { let errorSpy; From 4267025f8f318519f19a5d5733fd9f23b395506d Mon Sep 17 00:00:00 2001 From: Mark de Dios <99303358+peanutenthusiast@users.noreply.github.com> Date: Wed, 13 Sep 2023 08:59:32 -0700 Subject: [PATCH 5/5] Remove empty string instantiationn Co-authored-by: Morgan Roderick <20321+mroderick@users.noreply.github.com> --- lib/sinon/default-behaviors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sinon/default-behaviors.js b/lib/sinon/default-behaviors.js index 901e2fb65..1be5f9af1 100644 --- a/lib/sinon/default-behaviors.js +++ b/lib/sinon/default-behaviors.js @@ -16,7 +16,7 @@ function throwsException(fake, error, message) { } else if (typeof error === "string") { fake.exceptionCreator = function () { const newException = new Error( - message || `Sinon-provided ${error}` || "" + message || `Sinon-provided ${error}` ); newException.name = error; return newException;