Skip to content

Commit fae4203

Browse files
authored
feat: [prefer-in-document] Detect and auto-fix 'toBeFalsy()' and 'toBeTruthy()' (#172)
1 parent 85a3a20 commit fae4203

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

docs/rules/prefer-in-document.md

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ expect(queryByText("foo")).toEqual(null);
2121
expect(queryByText("foo")).not.toEqual(null);
2222
expect(queryByText("foo")).toBeDefined();
2323
expect(queryByText("foo")).not.toBeDefined();
24+
expect(queryByText("foo")).toBeTruthy();
25+
expect(queryByText("foo")).not.toBeTruthy();
26+
expect(queryByText("foo")).toBeFalsy();
27+
expect(queryByText("foo")).not.toBeFalsy();
2428

2529
const foo = screen.getByText("foo");
2630
expect(foo).toHaveLength(1);

src/__tests__/lib/rules/prefer-in-document.js

+16
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,22 @@ const invalid = [
268268
`expect(queryByText('foo')) .not .toBeDefined()`,
269269
`expect(queryByText('foo')) .not .toBeInTheDocument()`
270270
),
271+
invalidCase(
272+
`expect(queryByText('foo')).toBeFalsy()`,
273+
`expect(queryByText('foo')).not.toBeInTheDocument()`
274+
),
275+
invalidCase(
276+
`expect(queryByText('foo')).not.toBeFalsy()`,
277+
`expect(queryByText('foo')).toBeInTheDocument()`
278+
),
279+
invalidCase(
280+
`expect(queryByText('foo')).toBeTruthy()`,
281+
`expect(queryByText('foo')).toBeInTheDocument()`
282+
),
283+
invalidCase(
284+
`expect(queryByText('foo')).not.toBeTruthy()`,
285+
`expect(queryByText('foo')).not.toBeInTheDocument()`
286+
),
271287
invalidCase(
272288
`let foo;
273289
foo = screen.queryByText('foo');

src/rules/prefer-in-document.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const meta = {
2626
function isAntonymMatcher(matcherNode, matcherArguments) {
2727
return (
2828
matcherNode.name === "toBeNull" ||
29+
matcherNode.name === "toBeFalsy" ||
2930
usesToBeOrToEqualWithNull(matcherNode, matcherArguments) ||
3031
usesToHaveLengthZero(matcherNode, matcherArguments)
3132
);
@@ -43,7 +44,7 @@ function usesToHaveLengthZero(matcherNode, matcherArguments) {
4344
}
4445

4546
export const create = (context) => {
46-
const alternativeMatchers = /^(toHaveLength|toBeDefined|toBeNull|toBe|toEqual)$/;
47+
const alternativeMatchers = /^(toHaveLength|toBeDefined|toBeNull|toBe|toEqual|toBeTruthy|toBeFalsy)$/;
4748
function getLengthValue(matcherArguments) {
4849
let lengthValue;
4950

0 commit comments

Comments
 (0)