Skip to content

Commit 3415744

Browse files
authored
fix(prefer-to-have-been-called-times): actually check that current matcher is toHaveLength (#1878)
1 parent 45b4b11 commit 3415744

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

src/rules/__tests__/prefer-to-have-been-called-times.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ ruleTester.run('prefer-to-have-been-called-times', rule, {
1818
'expect(fn).toHaveBeenCalledTimes(0);',
1919
'expect(fn);',
2020
'expect(method.mock.calls[0][0]).toStrictEqual(value);',
21+
'expect(fn.mock.length).toEqual(1);',
22+
'expect(fn.mock.calls).toEqual([]);',
23+
'expect(fn.mock.calls).toContain(1, 2, 3);',
2124
],
2225

2326
invalid: [

src/rules/__tests__/prefer-to-have-been-called.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ ruleTester.run('prefer-to-have-been-called', rule, {
1818
'expect(method).not.toHaveBeenCalledTimes(...x)',
1919
'expect(a);',
2020
'expect(method).not.resolves.toHaveBeenCalledTimes(0);',
21+
'expect(method).toBe([])',
22+
'expect(fn.mock.calls).toEqual([])',
23+
'expect(fn.mock.calls).toContain(1, 2, 3)',
2124
],
2225

2326
invalid: [

src/rules/prefer-to-have-been-called-times.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export default createRule({
3030
return;
3131
}
3232

33+
const { matcher } = jestFnCall;
34+
35+
if (!isSupportedAccessor(matcher, 'toHaveLength')) {
36+
return;
37+
}
38+
3339
const [argument] = expect.arguments;
3440

3541
// check if the last property in the chain is `calls`
@@ -50,8 +56,6 @@ export default createRule({
5056
return;
5157
}
5258

53-
const { matcher } = jestFnCall;
54-
5559
context.report({
5660
messageId: 'preferMatcher',
5761
node: matcher,

0 commit comments

Comments
 (0)