Skip to content

Commit 335e073

Browse files
authored
fix(expect): fix validation of nth param in toHaveBeenNthCalledWith matcher (denoland#5227)
1 parent 9a4dfff commit 335e073

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

expect/_matchers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ export function toHaveBeenNthCalledWith(
605605
...expected: unknown[]
606606
): MatchResult {
607607
if (nth < 1) {
608-
new Error(`nth must be greater than 0. ${nth} was given.`);
608+
throw new Error(`nth must be greater than 0. ${nth} was given.`);
609609
}
610610

611611
const calls = getMockCalls(context.value);

expect/_to_have_been_nth_called_with_test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,15 @@ Deno.test("expect().toHaveBeenNthCalledWith() should throw when mock call does n
4242
'Expected the n-th call (n=2) of mock function is with "hello", but the n-th call does not exist.',
4343
);
4444
});
45+
46+
Deno.test("expect().toHaveBeenNthCalledWith() throw when n is not a positive integer", () => {
47+
const mockFn = fn();
48+
49+
assertThrows(
50+
() => {
51+
expect(mockFn).toHaveBeenNthCalledWith(0, "hello");
52+
},
53+
Error,
54+
"nth must be greater than 0. 0 was given.",
55+
);
56+
});

0 commit comments

Comments
 (0)