Skip to content

Commit dff1cb4

Browse files
authored
fix(prefer-called-with): handle resolves and rejects modifiers correctly (#1143)
1 parent 9381322 commit dff1cb4

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/rules/__tests__/prefer-called-with.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ ruleTester.run('prefer-called-with', rule, {
1010
'expect(fn).toBeCalledWith(expect.anything());',
1111
'expect(fn).toHaveBeenCalledWith(expect.anything());',
1212
'expect(fn).not.toBeCalled();',
13+
'expect(fn).rejects.not.toBeCalled();',
1314
'expect(fn).not.toHaveBeenCalled();',
1415
'expect(fn).not.toBeCalledWith();',
1516
'expect(fn).not.toHaveBeenCalledWith();',
17+
'expect(fn).resolves.not.toHaveBeenCalledWith();',
1618
'expect(fn).toBeCalledTimes(0);',
1719
'expect(fn).toHaveBeenCalledTimes(0);',
1820
'expect(fn);',
@@ -30,6 +32,17 @@ ruleTester.run('prefer-called-with', rule, {
3032
},
3133
],
3234
},
35+
{
36+
code: 'expect(fn).resolves.toBeCalled();',
37+
errors: [
38+
{
39+
messageId: 'preferCalledWith',
40+
data: { name: 'toBeCalled' },
41+
column: 21,
42+
line: 1,
43+
},
44+
],
45+
},
3346
{
3447
code: 'expect(fn).toHaveBeenCalled();',
3548
errors: [

src/rules/prefer-called-with.ts

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { createRule, isExpectCall, parseExpectCall } from './utils';
1+
import {
2+
ModifierName,
3+
createRule,
4+
isExpectCall,
5+
parseExpectCall,
6+
} from './utils';
27

38
export default createRule({
49
name: __filename,
@@ -25,15 +30,20 @@ export default createRule({
2530

2631
const { modifier, matcher } = parseExpectCall(node);
2732

28-
// Could check resolves/rejects here but not a likely idiom.
29-
if (matcher && !modifier) {
30-
if (['toBeCalled', 'toHaveBeenCalled'].includes(matcher.name)) {
31-
context.report({
32-
data: { name: matcher.name }, // todo: rename to 'matcherName'
33-
messageId: 'preferCalledWith',
34-
node: matcher.node.property,
35-
});
36-
}
33+
if (
34+
!matcher ||
35+
modifier?.name === ModifierName.not ||
36+
modifier?.negation
37+
) {
38+
return;
39+
}
40+
41+
if (['toBeCalled', 'toHaveBeenCalled'].includes(matcher.name)) {
42+
context.report({
43+
data: { name: matcher.name },
44+
messageId: 'preferCalledWith',
45+
node: matcher.node.property,
46+
});
3747
}
3848
},
3949
};

0 commit comments

Comments
 (0)