diff --git a/src/rules/__tests__/prefer-called-with.test.ts b/src/rules/__tests__/prefer-called-with.test.ts index cb118739d..e3bd95fa9 100644 --- a/src/rules/__tests__/prefer-called-with.test.ts +++ b/src/rules/__tests__/prefer-called-with.test.ts @@ -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);', @@ -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: [ diff --git a/src/rules/prefer-called-with.ts b/src/rules/prefer-called-with.ts index 70e207f6e..a30ac5776 100644 --- a/src/rules/prefer-called-with.ts +++ b/src/rules/prefer-called-with.ts @@ -1,4 +1,9 @@ -import { createRule, isExpectCall, parseExpectCall } from './utils'; +import { + ModifierName, + createRule, + isExpectCall, + parseExpectCall, +} from './utils'; export default createRule({ name: __filename, @@ -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, + }); } }, };