Skip to content

Commit c339605

Browse files
fix(2525): ensure non empty message when error of type string is passed, but no message (#2544)
Co-authored-by: Morgan Roderick <[email protected]>
1 parent 8d1f85b commit c339605

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

lib/sinon/default-behaviors.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ function throwsException(fake, error, message) {
1515
fake.exceptionCreator = error;
1616
} else if (typeof error === "string") {
1717
fake.exceptionCreator = function () {
18-
const newException = new Error(message || "");
18+
const newException = new Error(
19+
message || `Sinon-provided ${error}`
20+
);
1921
newException.name = error;
2022
return newException;
2123
};

test/proxy-call-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ describe("sinonSpy.call", function () {
11341134

11351135
assert.equals(
11361136
object.doIt.getCall(0).toString().replace(/ at.*/g, ""),
1137-
"doIt() !TypeError"
1137+
"doIt() !TypeError(Sinon-provided TypeError)"
11381138
);
11391139
});
11401140

test/stub-test.js

+15
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,21 @@ describe("stub", function () {
940940
assert.contains(stub.firstCall.toString(), "not implemented");
941941
});
942942

943+
it("creates a non empty error message when error is a string and no message is passed", function () {
944+
const stub = createStub();
945+
946+
stub.withArgs(1).throws("TypeError");
947+
948+
assert.exception(
949+
function () {
950+
stub(1);
951+
},
952+
{
953+
message: "Sinon-provided TypeError",
954+
}
955+
);
956+
});
957+
943958
describe("lazy instantiation of exceptions", function () {
944959
let errorSpy;
945960
beforeEach(function () {

0 commit comments

Comments
 (0)