eslint-plugin-testing-library: False positives with testing-library/prefer-presence-queries

Plugin version

v5.0.0

ESLint version

v8.2.0

Node.js version

14.18.0

npm/yarn version

1.22.17

Operating system

Big Sur

Bug description

Generally I like this rule but we have to disable it in quite a few places due to false positives with toBeNull.

Steps to reproduce

Examples:

expect(queryTooltip(screen.getByText('Some text'))).toBeNull();
expect(within(screen.getByRole('dialog')).queryByText('Some text')).toBeNull();
expect(screen.getByText('Some text').getAttribute('disabled')).toBeNull();

Error output/screenshots

No response

ESLint configuration

    {
      files: ['src/**/*.test.[tj]s?(x)', 'src/test/**/*.[tj]s?(x)'],
      env: {
        jest: true,
        browser: true,
      },
      plugins: ['eslint-plugin-jest', 'eslint-plugin-testing-library'],
      extends: ['plugin:jest/recommended', 'plugin:testing-library/react'],
      rules: {
        // warning are under migration
        'testing-library/prefer-screen-queries': 'warn',
        'testing-library/no-render-in-setup': 'warn',
        'testing-library/render-result-naming-convention': 'warn',
        'testing-library/no-wait-for-multiple-assertions': 'warn',
        'testing-library/no-node-access': 'off'
      },
    },

Rule(s) affected

testing-library/prefer-presence-queries

Anything else?

No response

Do you want to submit a pull request to fix this bug?

No

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 17 (12 by maintainers)

Commits related to this issue

Most upvoted comments

@Belco90: I started looking into this issue, and there’s something wrong with getDeepestIdentifierNode function, either the JS Doc comment is wrong, or the implementation is buggy.

For example…

// This is just pseudo code, I'm getting this result from running rule unit tests and printing out this stuff
const a = rtl.within('foo').getByRole('button')
const rtlNode = ... // get this somehow
getDeepestIdentifierNode(rtlNode).name // returns 'rtl', not 'getRoleButton' as the JS Doc says

Not sure if I understood your example correctly. I’m confused about the const rtlNode = ... // get this somehow line, could you elaborate on this?

The JSDoc description is correct, so you should get the identifier for the last chained node. If that’s not the case, we have a bug, but I didn’t fully understand your example.

since the node-utils functions aren’t unit tested, I was thinking would that they catch stuff like this in the future

They are! It’s difficult to spot that. If you go to the tests/create-testing-library-rule.test.ts file, you’ll see tests for a fake rule. We use this as some sort of unit tests for our internal utils, mainly.

Hey @josias-r, this is a different problem. Could you move it to a different issue? Thanks.