Skip to content

Commit

Permalink
feat: [prefer-in-document] Detect and auto-fix 'toBeFalsy()' and 'toB…
Browse files Browse the repository at this point in the history
…eTruthy()' (#172)
  • Loading branch information
julienw authored May 10, 2021
1 parent 85a3a20 commit fae4203
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/rules/prefer-in-document.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ expect(queryByText("foo")).toEqual(null);
expect(queryByText("foo")).not.toEqual(null);
expect(queryByText("foo")).toBeDefined();
expect(queryByText("foo")).not.toBeDefined();
expect(queryByText("foo")).toBeTruthy();
expect(queryByText("foo")).not.toBeTruthy();
expect(queryByText("foo")).toBeFalsy();
expect(queryByText("foo")).not.toBeFalsy();

const foo = screen.getByText("foo");
expect(foo).toHaveLength(1);
Expand Down
16 changes: 16 additions & 0 deletions src/__tests__/lib/rules/prefer-in-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,22 @@ const invalid = [
`expect(queryByText('foo')) .not .toBeDefined()`,
`expect(queryByText('foo')) .not .toBeInTheDocument()`
),
invalidCase(
`expect(queryByText('foo')).toBeFalsy()`,
`expect(queryByText('foo')).not.toBeInTheDocument()`
),
invalidCase(
`expect(queryByText('foo')).not.toBeFalsy()`,
`expect(queryByText('foo')).toBeInTheDocument()`
),
invalidCase(
`expect(queryByText('foo')).toBeTruthy()`,
`expect(queryByText('foo')).toBeInTheDocument()`
),
invalidCase(
`expect(queryByText('foo')).not.toBeTruthy()`,
`expect(queryByText('foo')).not.toBeInTheDocument()`
),
invalidCase(
`let foo;
foo = screen.queryByText('foo');
Expand Down
3 changes: 2 additions & 1 deletion src/rules/prefer-in-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const meta = {
function isAntonymMatcher(matcherNode, matcherArguments) {
return (
matcherNode.name === "toBeNull" ||
matcherNode.name === "toBeFalsy" ||
usesToBeOrToEqualWithNull(matcherNode, matcherArguments) ||
usesToHaveLengthZero(matcherNode, matcherArguments)
);
Expand All @@ -43,7 +44,7 @@ function usesToHaveLengthZero(matcherNode, matcherArguments) {
}

export const create = (context) => {
const alternativeMatchers = /^(toHaveLength|toBeDefined|toBeNull|toBe|toEqual)$/;
const alternativeMatchers = /^(toHaveLength|toBeDefined|toBeNull|toBe|toEqual|toBeTruthy|toBeFalsy)$/;
function getLengthValue(matcherArguments) {
let lengthValue;

Expand Down

0 comments on commit fae4203

Please sign in to comment.