Skip to content

Commit 34da6c1

Browse files
authored
fix(no-get-by-for-checking-element-not-present): false positives for negated matchers (#84)
1 parent 481bc2a commit 34da6c1

4 files changed

+16
-4
lines changed

docs/rules/no-get-by-for-checking-element-not-present.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Examples of **incorrect** code for this rule:
2323
test('some test', () => {
2424
const { getByText } = render(<App />);
2525
expect(getByText('Foo')).not.toBeInTheDocument();
26+
expect(getByText('Foo')).not.toBeTruthy();
2627
expect(getByText('Foo')).toBeFalsy();
2728
expect(getByText('Foo')).toBeNull();
2829
});
@@ -41,6 +42,7 @@ Examples of **correct** code for this rule:
4142
test('some test', () => {
4243
const { getByText } = render(<App />);
4344
expect(getByText('Foo')).toBeInTheDocument();
45+
expect(getByText('Foo')).not.toBeDisabled();
4446
expect(queryByText('Foo')).not.toBeInTheDocument();
4547
expect(queryByText('Foo')).toBeFalsy();
4648
});

lib/rules/no-get-by-for-checking-element-not-present.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
const { getDocsUrl } = require('../utils');
44

5-
const falsyMatchers = ['toBeNull', 'toBeFalsy'];
5+
const FALSY_MATCHERS = ['toBeNull', 'toBeFalsy'];
6+
const NOT_ALLOWED_NEGATED_MATCHERS = [
7+
'toBeInTheDocument',
8+
'toBeTruthy',
9+
'toBeDefined',
10+
];
611

712
module.exports = {
813
meta: {
@@ -34,15 +39,15 @@ module.exports = {
3439
if (matcher === 'not') {
3540
const negatedMatcher = expectStatement.parent.property.name;
3641

37-
if (!falsyMatchers.includes(negatedMatcher)) {
42+
if (NOT_ALLOWED_NEGATED_MATCHERS.includes(negatedMatcher)) {
3843
return context.report({
3944
node,
4045
messageId: 'expectQueryBy',
4146
});
4247
}
4348
}
4449

45-
if (falsyMatchers.includes(matcher)) {
50+
if (FALSY_MATCHERS.includes(matcher)) {
4651
return context.report({
4752
node,
4853
messageId: 'expectQueryBy',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"format": "prettier --write README.md {lib,docs,tests}/**/*.{js,md}",
3131
"test:local": "jest",
3232
"test:ci": "jest --coverage",
33+
"test:watch": "npm run test:local -- --watch",
3334
"test": "is-ci test:ci test:local",
3435
"semantic-release": "semantic-release"
3536
},

tests/lib/rules/no-get-by-for-checking-element-not-present.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const getInvalidAssertion = (query, matcher) =>
2727
errors: [{ messageId: 'expectQueryBy' }],
2828
}));
2929

30-
ruleTester.run('prefer-expect-query-by', rule, {
30+
ruleTester.run('no-get-by-for-checking-element-not-present', rule, {
3131
valid: [
3232
...getByQueries.reduce(
3333
(validRules, queryName) => [
@@ -38,6 +38,8 @@ ruleTester.run('prefer-expect-query-by', rule, {
3838
...getValidAssertion(queryName, '.toEqual("World")'),
3939
...getValidAssertion(queryName, '.not.toBeFalsy()'),
4040
...getValidAssertion(queryName, '.not.toBeNull()'),
41+
...getValidAssertion(queryName, '.not.toBeDisabled()'),
42+
...getValidAssertion(queryName, '.not.toHaveClass("btn")'),
4143
],
4244
[]
4345
),
@@ -47,6 +49,7 @@ ruleTester.run('prefer-expect-query-by', rule, {
4749
...getValidAssertion(queryName, '.not.toBeInTheDocument()'),
4850
...getValidAssertion(queryName, '.toBeNull()'),
4951
...getValidAssertion(queryName, '.not.toBeTruthy()'),
52+
...getValidAssertion(queryName, '.not.toBeDefined()'),
5053
...getValidAssertion(queryName, '.toBeFalsy()'),
5154
{
5255
code: `(async () => {
@@ -77,6 +80,7 @@ ruleTester.run('prefer-expect-query-by', rule, {
7780
...getInvalidAssertion(queryName, '.not.toBeInTheDocument()'),
7881
...getInvalidAssertion(queryName, '.toBeNull()'),
7982
...getInvalidAssertion(queryName, '.not.toBeTruthy()'),
83+
...getInvalidAssertion(queryName, '.not.toBeDefined()'),
8084
...getInvalidAssertion(queryName, '.toBeFalsy()'),
8185
{
8286
code: `(async () => {

0 commit comments

Comments
 (0)