Skip to content

Commit a68dac1

Browse files
authored
fix: throw a clear error when throwArg index equals the argument count (#2743)
* fix: throw a clear error when throwArg index equals the argument count spyCall.throwArg(pos) guarded with pos > this.args.length, so calling it with pos equal to the number of recorded arguments slipped past the guard and reached throw this.args[pos], throwing undefined instead of the intended TypeError. A thrown undefined cannot be inspected as an Error and is reported by test frameworks as no exception thrown. Use >= to match ensureArgs in behavior.js and the sibling callArg helpers, which already reject an out-of-range index with a clear error.
1 parent 9ea504e commit a68dac1

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/sinon/proxy-call.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ const callProto = {
140140
},
141141

142142
throwArg: function (pos) {
143-
if (pos > this.args.length) {
143+
if (pos >= this.args.length) {
144144
throw new TypeError(
145145
`Not enough arguments: ${pos} required but only ${this.args.length} present`,
146146
);

test/src/proxy-call-test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,55 @@ describe("sinonSpy.call", function () {
613613
});
614614
});
615615

616+
describe("call.throwArg", function () {
617+
beforeEach(spyCallCallSetup);
618+
619+
it("throws argument at specified index", function () {
620+
const expectedError = new TypeError("catpants");
621+
this.args.push(1, expectedError, 2);
622+
const call = this.call;
623+
624+
assert.exception(
625+
function () {
626+
call.throwArg(1);
627+
},
628+
{ message: "catpants" },
629+
);
630+
});
631+
632+
it("throws if index equals the number of arguments", function () {
633+
this.args.push(1, 2);
634+
const call = this.call;
635+
636+
assert.exception(
637+
function () {
638+
call.throwArg(2);
639+
},
640+
{
641+
name: "TypeError",
642+
message:
643+
"Not enough arguments: 2 required but only 2 present",
644+
},
645+
);
646+
});
647+
648+
it("throws if there aren't enough arguments", function () {
649+
this.args.push(1, 2);
650+
const call = this.call;
651+
652+
assert.exception(
653+
function () {
654+
call.throwArg(3);
655+
},
656+
{
657+
name: "TypeError",
658+
message:
659+
"Not enough arguments: 3 required but only 2 present",
660+
},
661+
);
662+
});
663+
});
664+
616665
describe("call.yieldTest", function () {
617666
beforeEach(spyCallCallSetup);
618667

0 commit comments

Comments
 (0)