Skip to content

Commit

Permalink
Make stub.rejects able to reject with string as reason
Browse files Browse the repository at this point in the history
refs #1679
  • Loading branch information
fatso83 committed Nov 5, 2023
1 parent 93db3ef commit 23ee88c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/sinon/default-behaviors.js
Expand Up @@ -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) {
Expand Down
17 changes: 2 additions & 15 deletions test/stub-test.js
Expand Up @@ -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()
Expand All @@ -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);
Expand All @@ -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();
Expand Down

0 comments on commit 23ee88c

Please sign in to comment.