react-testing-library icon indicating copy to clipboard operation
react-testing-library copied to clipboard

Update to v14 breaks @testing-library/user-event on Vitest

Open wojtekmaj opened this issue 1 year ago • 28 comments

What you did:

A simple update from v13 to v14 broke my Vitest-based test where I was using await user.click(...) as the promise no longer resolves.

Reproduction:

Run repo at the following commit: https://github.com/wojtekmaj/react-async-button/commit/fa41b3b9900a25d76141bcf2080f94f7ee5f5dee

Suggested solution:

After long debug session, I have determined that

  • Monkey patching asyncWrapper to be just cb => cb() resolves the issue.
  • Removing the following code added in testing-library/react-testing-library#1137 resolves the issue:

https://github.com/testing-library/react-testing-library/blob/f78839bf4147a777a823e33a429bcf5de9562f9e/src/pure.js#L41-L52

  • Moving if (jestFakeTimersAreEnabled()) { ... } to wrap the entire block mentioned above resolves the issue.
  • Calling vi.advanceTimersByTime(0); manually after user.click(...) but before awaiting returned promise, even multiple times, does NOT help
  • The only workaround that worked for me was this: https://github.com/wojtekmaj/react-async-button/commit/2d26f217a375b7020ddf42f76891254586fc3ce4

So my suggestion is to:

  • Roll back the fix and perhaps reintroduce when advanceTimers will be configurable and not jest dependent
  • OR move if (jestFakeTimersAreEnabled()) { ... } to wrap the entire block mentioned above, acknowledging that the fix is now Jest-only.

wojtekmaj avatar Mar 21 '23 23:03 wojtekmaj