testing-library / user-event

🐕 Simulate user events
https://testing-library.com/user-event
MIT License
2.18k stars 245 forks source link

"act" is necessary with Fake Timers even though (IIUC) shouldn't be #1218

Open mitchhentgesspotify opened 4 months ago

mitchhentgesspotify commented 4 months ago

Reproduction example

https://codesandbox.io/p/devbox/9r8lr2

Prerequisites

In my linked repro, this test will fail unless the "user input" done by the test happens in an act()

So, within the linked sandbox:

  1. Run pnpm test, the test will fail
  2. Tweak MyForm.test.js and uncomment the act(...) call. Running pnpm test will now succeed.

Expected behavior

The test should pass without needing to wrap the user input in act(...), because according to this comment:

No, wrapping with act is neither required nor recommended.

Actual behavior

If the user input isn't wrapped in act(...), then, even though the useState() value was updated, a re-render isn't triggered before the test continues, causing it to fail.

User-event version

14.5.2

Environment

Repro'd in a sandbox even though it's a bit tricky these days 😅

Additional context

No response