Skip to content

fix(no-get-by-for-checking-element-not-present): false positives for negated matchers #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2020
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
2 changes: 2 additions & 0 deletions docs/rules/no-get-by-for-checking-element-not-present.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Examples of **incorrect** code for this rule:
test('some test', () => {
const { getByText } = render(<App />);
expect(getByText('Foo')).not.toBeInTheDocument();
expect(getByText('Foo')).not.toBeTruthy();
expect(getByText('Foo')).toBeFalsy();
expect(getByText('Foo')).toBeNull();
});
Expand All @@ -41,6 +42,7 @@ Examples of **correct** code for this rule:
test('some test', () => {
const { getByText } = render(<App />);
expect(getByText('Foo')).toBeInTheDocument();
expect(getByText('Foo')).not.toBeDisabled();
expect(queryByText('Foo')).not.toBeInTheDocument();
expect(queryByText('Foo')).toBeFalsy();
});
Expand Down
11 changes: 8 additions & 3 deletions lib/rules/no-get-by-for-checking-element-not-present.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

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

const falsyMatchers = ['toBeNull', 'toBeFalsy'];
const FALSY_MATCHERS = ['toBeNull', 'toBeFalsy'];
const NOT_ALLOWED_NEGATED_MATCHERS = [
'toBeInTheDocument',
'toBeTruthy',
'toBeDefined',
];

module.exports = {
meta: {
Expand Down Expand Up @@ -34,15 +39,15 @@ module.exports = {
if (matcher === 'not') {
const negatedMatcher = expectStatement.parent.property.name;

if (!falsyMatchers.includes(negatedMatcher)) {
if (NOT_ALLOWED_NEGATED_MATCHERS.includes(negatedMatcher)) {
return context.report({
node,
messageId: 'expectQueryBy',
});
}
}

if (falsyMatchers.includes(matcher)) {
if (FALSY_MATCHERS.includes(matcher)) {
return context.report({
node,
messageId: 'expectQueryBy',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"format": "prettier --write README.md {lib,docs,tests}/**/*.{js,md}",
"test:local": "jest",
"test:ci": "jest --coverage",
"test:watch": "npm run test:local -- --watch",
"test": "is-ci test:ci test:local",
"semantic-release": "semantic-release"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const getInvalidAssertion = (query, matcher) =>
errors: [{ messageId: 'expectQueryBy' }],
}));

ruleTester.run('prefer-expect-query-by', rule, {
ruleTester.run('no-get-by-for-checking-element-not-present', rule, {
valid: [
...getByQueries.reduce(
(validRules, queryName) => [
Expand All @@ -38,6 +38,8 @@ ruleTester.run('prefer-expect-query-by', rule, {
...getValidAssertion(queryName, '.toEqual("World")'),
...getValidAssertion(queryName, '.not.toBeFalsy()'),
...getValidAssertion(queryName, '.not.toBeNull()'),
...getValidAssertion(queryName, '.not.toBeDisabled()'),
...getValidAssertion(queryName, '.not.toHaveClass("btn")'),
],
[]
),
Expand All @@ -47,6 +49,7 @@ ruleTester.run('prefer-expect-query-by', rule, {
...getValidAssertion(queryName, '.not.toBeInTheDocument()'),
...getValidAssertion(queryName, '.toBeNull()'),
...getValidAssertion(queryName, '.not.toBeTruthy()'),
...getValidAssertion(queryName, '.not.toBeDefined()'),
...getValidAssertion(queryName, '.toBeFalsy()'),
{
code: `(async () => {
Expand Down Expand Up @@ -77,6 +80,7 @@ ruleTester.run('prefer-expect-query-by', rule, {
...getInvalidAssertion(queryName, '.not.toBeInTheDocument()'),
...getInvalidAssertion(queryName, '.toBeNull()'),
...getInvalidAssertion(queryName, '.not.toBeTruthy()'),
...getInvalidAssertion(queryName, '.not.toBeDefined()'),
...getInvalidAssertion(queryName, '.toBeFalsy()'),
{
code: `(async () => {
Expand Down