Skip to content

Commit

Permalink
fix(prefer-in-document): handle toHaveLength without any arguments …
Browse files Browse the repository at this point in the history
…and with trailing commas (#276)

* fix(prefer-in-document): crash when on matcher args in toHaveLength

* fix(prefer-in-document): properly remove all matcher args and commas

* fix(prefer-in-document): update comment on comma removal logic
  • Loading branch information
berkinanik committed Nov 10, 2022
1 parent 4299024 commit 5ab24d9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
80 changes: 80 additions & 0 deletions src/__tests__/lib/rules/prefer-in-document.js
Expand Up @@ -51,6 +51,10 @@ const valid = [
expect(foo).not.toHaveLength(0)`,
`let foo;
expect(foo).toHaveLength(1);`,
`let foo;
expect(foo).toHaveLength()`,
`let foo;
expect(foo).toHaveLength(1, 2, 3)`,
`expect(screen.notAQuery('foo-bar')).toHaveLength(1)`,
`expect(screen.getAllByText('foo-bar')).toHaveLength(2)`,
`import foo from "./foo";
Expand Down Expand Up @@ -99,6 +103,82 @@ const valid = [
expect(element).toBeInTheDocument`,
];
const invalid = [
invalidCase(
`expect(screen.getByText('foo')).toHaveLength()`,
`expect(screen.getByText('foo')).not.toBeInTheDocument()`
),
invalidCase(
`expect(screen.getAllByText('foo')).toHaveLength()`,
`expect(screen.getByText('foo')).not.toBeInTheDocument()`
),
invalidCase(
`expect(screen.getByRole('foo')).toHaveLength()`,
`expect(screen.getByRole('foo')).not.toBeInTheDocument()`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength()`,
`expect(screen.getByRole('foo')).not.toBeInTheDocument()`
),
invalidCase(
`expect(screen.getByRole('foo')).toHaveLength(0,2,3)`,
`expect(screen.getByRole('foo')).not.toBeInTheDocument()`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(0,2,3,)`,
`expect(screen.getByRole('foo')).not.toBeInTheDocument()`
),
invalidCase(
`expect(screen.getByRole('foo')).toHaveLength(1,2,3)`,
`expect(screen.getByRole('foo')).toBeInTheDocument()`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(1,2,3,)`,
`expect(screen.getByRole('foo')).toBeInTheDocument()`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(0//comment
)`,
`expect(screen.getByRole('foo')).not.toBeInTheDocument(//comment
)`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(1,//comment
)`,
`expect(screen.getByRole('foo')).toBeInTheDocument(//comment
)`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(0,2,3//comment
)`,
`expect(screen.getByRole('foo')).not.toBeInTheDocument(//comment
)`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(1,2,3,//comment
)`,
`expect(screen.getByRole('foo')).toBeInTheDocument(//comment
)`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(0,2,//comment
3,4)`,
`expect(screen.getByRole('foo')).not.toBeInTheDocument(//comment
)`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(1,2,//comment
3,4,)`,
`expect(screen.getByRole('foo')).toBeInTheDocument(//comment
)`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(0,2/*comment*/,3)`,
`expect(screen.getByRole('foo')).not.toBeInTheDocument(/*comment*/)`
),
invalidCase(
`expect(screen.getAllByRole('foo')).toHaveLength(1,2,/*comment*/3,)`,
`expect(screen.getByRole('foo')).toBeInTheDocument(/*comment*/)`
),
invalidCase(
`expect(screen.getByText('foo')).toHaveLength(1)`,
`expect(screen.getByText('foo')).toBeInTheDocument()`
Expand Down
13 changes: 12 additions & 1 deletion src/rules/prefer-in-document.js
Expand Up @@ -40,7 +40,12 @@ function usesToBeOrToEqualWithNull(matcherNode, matcherArguments) {
}

function usesToHaveLengthZero(matcherNode, matcherArguments) {
return matcherNode.name === "toHaveLength" && matcherArguments[0].value === 0;
// matcherArguments.length === 0: toHaveLength() will cause jest matcher error
// matcherArguments[0].value: toHaveLength(0, ...) means zero length
return (
matcherNode.name === "toHaveLength" &&
(matcherArguments.length === 0 || matcherArguments[0].value === 0)
);
}

export const create = (context) => {
Expand Down Expand Up @@ -117,6 +122,12 @@ export const create = (context) => {

// Remove any arguments in the matcher
for (const argument of Array.from(matcherArguments)) {
const sourceCode = context.getSourceCode();
const token = sourceCode.getTokenAfter(argument);
if (token.value === "," && token.type === "Punctuator") {
// Remove commas if toHaveLength had more than one argument or a trailing comma
operations.push(fixer.replaceText(token, ""));
}
operations.push(fixer.remove(argument));
}

Expand Down

0 comments on commit 5ab24d9

Please sign in to comment.