Skip to content

Incorrect prefer-find-by autofix when using within inside waitFor #536

Open
@atsikov

Description

@atsikov

Plugin version

v5.0.3

ESLint version

v8.7.0

Node.js version

v16.13.1

npm/yarn version

pnpm v5.18.0

Operating system

macOS Big Sur 11.6.2

Bug description

await waitFor blocks with within and chained getBy... queries are not correctly autofixed

Steps to reproduce

The following snippet raises a prefer-find-by error

const btnProceed = await waitFor(() =>
  within(screen.getByRole('dialog', { name: 'Confirmation' })).getByRole(
    'button',
    { name: 'Proceed' },
  ),
)

Rule autofixer produces the following code, losing a query for the button itself

const btnProceed = await screen.findByRole('dialog', { name: 'Confirmation' })

Error output/screenshots

No response

ESLint configuration

'testing-library/await-async-query': ['warn'],
'testing-library/await-async-utils': ['warn'],
'testing-library/no-await-sync-query': ['warn'],
'testing-library/no-debugging-utils': ['warn'],
'testing-library/no-dom-import': ['warn'],
'testing-library/no-manual-cleanup': ['warn'],
'testing-library/no-wait-for-empty-callback': ['warn'],
'testing-library/prefer-explicit-assert': ['warn', { includeFindQueries: false }],
'testing-library/prefer-find-by': ['warn'],
'testing-library/prefer-presence-queries': ['warn'],
'testing-library/prefer-screen-queries': ['warn'],
'testing-library/prefer-wait-for': ['warn'],

Rule(s) affected

testing-library/prefer-find-by

Anything else?

It should not be a difficult thing to fix, and I could submit a PR, however I'm not sure what's a better way to approach it.
Generally, there could be 3 different solutions:

  1. Do not trigger prefer-find-by for waitFor assertions like within(screen.getBy...()).getBy...()
  2. Autofix as await within(await screen.findBy...(...)).findBy...(...)
  3. Trigger an error but do not autofix such cases

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

Yes, but need help

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions