Skip to content

Commit

Permalink
fix(prefer-called-with): handle resolves and rejects modifiers co…
Browse files Browse the repository at this point in the history
…rrectly (#1143)
  • Loading branch information
G-Rath authored Jun 5, 2022
1 parent 9381322 commit dff1cb4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/rules/__tests__/prefer-called-with.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ ruleTester.run('prefer-called-with', rule, {
'expect(fn).toBeCalledWith(expect.anything());',
'expect(fn).toHaveBeenCalledWith(expect.anything());',
'expect(fn).not.toBeCalled();',
'expect(fn).rejects.not.toBeCalled();',
'expect(fn).not.toHaveBeenCalled();',
'expect(fn).not.toBeCalledWith();',
'expect(fn).not.toHaveBeenCalledWith();',
'expect(fn).resolves.not.toHaveBeenCalledWith();',
'expect(fn).toBeCalledTimes(0);',
'expect(fn).toHaveBeenCalledTimes(0);',
'expect(fn);',
Expand All @@ -30,6 +32,17 @@ ruleTester.run('prefer-called-with', rule, {
},
],
},
{
code: 'expect(fn).resolves.toBeCalled();',
errors: [
{
messageId: 'preferCalledWith',
data: { name: 'toBeCalled' },
column: 21,
line: 1,
},
],
},
{
code: 'expect(fn).toHaveBeenCalled();',
errors: [
Expand Down
30 changes: 20 additions & 10 deletions src/rules/prefer-called-with.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { createRule, isExpectCall, parseExpectCall } from './utils';
import {
ModifierName,
createRule,
isExpectCall,
parseExpectCall,
} from './utils';

export default createRule({
name: __filename,
Expand All @@ -25,15 +30,20 @@ export default createRule({

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

// Could check resolves/rejects here but not a likely idiom.
if (matcher && !modifier) {
if (['toBeCalled', 'toHaveBeenCalled'].includes(matcher.name)) {
context.report({
data: { name: matcher.name }, // todo: rename to 'matcherName'
messageId: 'preferCalledWith',
node: matcher.node.property,
});
}
if (
!matcher ||
modifier?.name === ModifierName.not ||
modifier?.negation
) {
return;
}

if (['toBeCalled', 'toHaveBeenCalled'].includes(matcher.name)) {
context.report({
data: { name: matcher.name },
messageId: 'preferCalledWith',
node: matcher.node.property,
});
}
},
};
Expand Down

0 comments on commit dff1cb4

Please sign in to comment.