Skip to content

Commit

Permalink
fix(2525): ensure non empty message when error of type string is pass…
Browse files Browse the repository at this point in the history
…ed, but no message (#2544)

Co-authored-by: Morgan Roderick <20321+mroderick@users.noreply.github.com>
  • Loading branch information
peanutenthusiast and mroderick authored Sep 13, 2023
1 parent 8d1f85b commit c339605
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/sinon/default-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
2 changes: 1 addition & 1 deletion test/proxy-call-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
);
});

Expand Down
15 changes: 15 additions & 0 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit c339605

Please sign in to comment.