Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/__tests__/lib/rules/prefer-in-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);`,
Expand Down
17 changes: 13 additions & 4 deletions src/rules/prefer-in-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 =
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
Expand Down