diff --git a/src/__tests__/lib/rules/prefer-in-document.js b/src/__tests__/lib/rules/prefer-in-document.js index 538abfc..05ff2f5 100644 --- a/src/__tests__/lib/rules/prefer-in-document.js +++ b/src/__tests__/lib/rules/prefer-in-document.js @@ -36,6 +36,9 @@ const valid = [ foo = somethingElse; expect(foo).toHaveLength(1);`, ]), + `expect().not.toBeNull()`, + `expect(myFunction()).toBe();`, + `expect(myFunction()).toHaveLength();`, `let foo; foo = "bar"; expect(foo).toHaveLength(1);`, diff --git a/src/rules/prefer-in-document.js b/src/rules/prefer-in-document.js index 8253af8..e6f4374 100644 --- a/src/rules/prefer-in-document.js +++ b/src/rules/prefer-in-document.js @@ -32,8 +32,10 @@ function isAntonymMatcher(matcherNode, matcherArguments) { } function usesToBeOrToEqualWithNull(matcherNode, matcherArguments) { - return (matcherNode.name === "toBe" || matcherNode.name === "toEqual") && - matcherArguments[0].value === null; + return ( + (matcherNode.name === "toBe" || matcherNode.name === "toEqual") && + matcherArguments[0].value === null + ); } function usesToHaveLengthZero(matcherNode, matcherArguments) { @@ -71,7 +73,7 @@ export const create = (context) => { if (!queryNode || (!queryNode.name && !queryNode.property)) return; // toHaveLength() is only invalid with 0 or 1 - if (matcherNode.name === "toHaveLength") { + if (matcherNode.name === "toHaveLength" && matcherArguments.length) { const lengthValue = getLengthValue(matcherArguments); // isNotToHaveLengthZero represents .not.toHaveLength(0) which is a valid use of toHaveLength const isNotToHaveLengthZero = @@ -89,7 +91,10 @@ export const create = (context) => { // toBe() or toEqual() are only invalid with null if (matcherNode.name === "toBe" || matcherNode.name === "toEqual") { - if (!usesToBeOrToEqualWithNull(matcherNode, matcherArguments)) { + if ( + !matcherArguments.length || + !usesToBeOrToEqualWithNull(matcherNode, matcherArguments) + ) { return; } } @@ -146,6 +151,10 @@ export const create = (context) => { [`CallExpression[callee.object.object.callee.name='expect'][callee.object.property.name='not'][callee.property.name=${alternativeMatchers}], CallExpression[callee.object.callee.name='expect'][callee.object.property.name='not'][callee.object.arguments.0.argument.callee.name=${alternativeMatchers}]`]( node ) { + if (!node.callee.object.object.arguments.length) { + return; + } + const arg = node.callee.object.object.arguments[0]; const queryNode = arg.type === "AwaitExpression" ? arg.argument.callee : arg.callee;