Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(2525): ensure non empty message when error of type string is passed, but no message #2544

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading