testing-library / eslint-plugin-testing-library

ESLint plugin to follow best practices and anticipate common mistakes when writing tests with Testing Library
https://npm.im/eslint-plugin-testing-library
MIT License
967 stars 135 forks source link

`await-async-events` produces false-positives #881

Closed kripod closed 2 months ago

kripod commented 4 months ago

Have you read the Troubleshooting section?

Yes

Plugin version

v6.2.0

ESLint version

v8.56.0

Node.js version

v20.10.0

package manager and version

pnpm 7.33.7

Operating system

macOS Sonoma, version 14.3.1

Bug description

The userEvent.setup() method is treated as if it were returning a Promise when wrapped by an arrow function expression.

Steps to reproduce

While exposing a getUser wrapper around userEvent.setup for reuse:

export const getUser = () =>
  userEvent.setup({
    advanceTimers: (msToRun: number) => jest.advanceTimersByTime(msToRun),
  });

Usages like:

const user = getUser();

Produce a false-positive error as follows:

error  Promise returned from `getUser` wrapper over async event method must be handled  testing-library/await-async-events

However, userEvent.setup isn‘t an async method, so there isn‘t anything to await.

Error output/screenshots

No response

ESLint configuration

{
  "extends": ["plugin:testing-library/react"]
}

or

{
  "plugins": ["testing-library"],
  "rules": {
    "testing-library/await-async-events": "error"
  }
}

Rule(s) affected

testing-library/await-async-events

Anything else?

No response

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

No

JulianG-TW commented 4 months ago

Just in case it helps.

If I change getUser to

export const getUser = () => {
  const user = userEvent.setup({
    advanceTimers: (msToRun: number) => jest.advanceTimersByTime(msToRun),
  });
  return user;
}

...then the false-positive error is gone.

Belco90 commented 3 months ago

Thanks for reporting @kripod

github-actions[bot] commented 2 months ago

:tada: This issue has been resolved in version 6.2.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: