From 23ee88c934adc5d941d05da5be5a57c1b215f0cf Mon Sep 17 00:00:00 2001 From: Carl-Erik Kopseng Date: Sat, 4 Nov 2023 11:30:22 +0100 Subject: [PATCH] Make stub.rejects able to reject with string as reason refs #1679 --- lib/sinon/default-behaviors.js | 2 +- test/stub-test.js | 17 ++--------------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/lib/sinon/default-behaviors.js b/lib/sinon/default-behaviors.js index dff8c9d81..f15d13646 100644 --- a/lib/sinon/default-behaviors.js +++ b/lib/sinon/default-behaviors.js @@ -213,7 +213,7 @@ const defaultBehaviors = { rejects: function rejects(fake, error, message) { let reason; - if (typeof error === "string") { + if (typeof error === "string" && typeof message !== "undefined") { reason = new Error(message || ""); reason.name = error; } else if (!error) { diff --git a/test/stub-test.js b/test/stub-test.js index 957cce9aa..c8f7c113d 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -349,7 +349,7 @@ describe("stub", function () { it("returns a promise which rejects for the specified reason", function () { const stub = createStub(); - const reason = new Error(); + const reason = "the reason"; stub.rejects(reason); return stub() @@ -367,7 +367,7 @@ describe("stub", function () { assert.same(stub.rejects({}), stub); }); - it("specifies exception message", function () { + it("specifies error type and exception message", function () { const stub = createStub(); const message = "Oh no!"; stub.rejects("Error", message); @@ -381,19 +381,6 @@ describe("stub", function () { }); }); - it("does not specify exception message if not provided", function () { - const stub = createStub(); - stub.rejects("Error"); - - return stub() - .then(function () { - referee.fail("Expected stub to reject"); - }) - .catch(function (reason) { - assert.equals(reason.message, ""); - }); - }); - it("rejects for a generic reason", function () { const stub = createStub(); stub.rejects();