Skip to content

Commit

Permalink
Make stub.throws be able to throw strings
Browse files Browse the repository at this point in the history
refs #1679
  • Loading branch information
fatso83 committed Nov 5, 2023
1 parent 23ee88c commit 9fef780
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 30 deletions.
6 changes: 2 additions & 4 deletions lib/sinon/default-behaviors.js
Expand Up @@ -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;
};
Expand Down
6 changes: 4 additions & 2 deletions test/proxy-call-test.js
Expand Up @@ -1126,15 +1126,17 @@ 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();
});

assert.equals(
object.doIt.getCall(0).toString().replace(/ at.*/g, ""),
"doIt() !TypeError(Sinon-provided TypeError)",
"doIt() !TypeError(some message)",
);
});

Expand Down
37 changes: 13 additions & 24 deletions test/stub-test.js
Expand Up @@ -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 () {
Expand All @@ -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();
Expand All @@ -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 () {
Expand Down

0 comments on commit 9fef780

Please sign in to comment.