user-event: Update of the @testing-library/react version to 14.0.0 throws "act" warnings
@testing-library/reactversion: 14.0.0- Testing Framework and version: jest 29.4.2
- DOM Environment: @testing-library/jest-dom: 5.16.5
Relevant code or config:
const saveButton = screen.getByRole('button', { name: /save/i });
await waitFor(() => expect(saveButton).toBeEnabled());
await testUser.click(saveButton);
What you did:
I upgraded the version of @testing-library/react from 13.4.0 to 14.0.0. And I started having problems in all the tests of my application. Before this update this did not happen.
What happened:
I’m getting more act warnings now with @testing-library@14.0.0.
Tested with await waitFor, and still throwing act warnings with userEvent that triggers a state change.

Reproduction:
You can fork to reproduce the issue: here: https://codesandbox.io/s/act-warning-wait-for-react-18-forked-6jurf2
Problem description:
The state update (setState()) seems to make the test library think that act is incomplete, even though it is. This happens for every unit test where an event is fired, such as userEvent.click() or userEvent.upload().
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 47
- Comments: 28 (8 by maintainers)
Commits related to this issue
- Update all Storybook deps For more info, see: https://github.com/testing-library/user-event/issues/1104 — committed to wolfpilot/wolfpilot.github.io by wolfpilot a year ago
I have the same issue, wrapping
testUser.click(saveButton)inside an act call resolves the issue. The problem is that this will have major impact on our codebase. I am wondering why this is now needed. It’s not listed in the release notes as breaking change so I’d say it should not break moving from 13.x.x to 14.0.0.+1, but I’ll add: I’m not just getting warnings, my tests are now failing after the upgrade. So it’s not just the verbosity of the warnings that are the problem, it’s the changed behavior.
I also noticed that I’m seeing these act warnings on updates I do via
@testing-library/user-event.I do like how testing-library takes care of wrapping its own calls in act in all the obvious places. I don’t think expecting callers to wrap all their calls in
actnow is a good solution. (And if that is the plan, then it’s a breaking change that would need to be in the docs.)I think @ph-fritsche hit the nail on the head here 🚀
I experienced lots of erroneous
actwarnings after updating@testing-library/reactto version 14.I’m assuming this is due to
@testing-library/reactdepending on@testing-library/domversion ^9.0.0, and@testing-library/user-eventhaving a peer dependency on@testing-library/domversion >=7.21.4. This led to@testing-library/domresolving to version 8.11.1.Overriding
@testing-library/domto version ^9.0.1 fixed all of the erroneousactwarnings I was experiencing as mentioned here.Thanks for the awesome library. Hopefully this helps someone else.
Thanks. I experienced the same. It was caused by eslint-plugin-jest-dom which is still using version 8. For yarn I needed to add a resolution to fix it
I see different issues being mixed up here.
If you use e.g.
@testing-library/reactand@testing-library/user-event, those need to resolve@testing-library/domto the same copy.See https://testing-library.com/docs/user-event/install:
With recent versions of
npmyou need to runnpm dedup.If you use fake timers, you need to tell
@testing-library/user-eventhow to wind the timer.See https://testing-library.com/docs/user-event/options#advancetimers:
With Jest’s fake timers you need to setup
user-eventwith{advanceTimers: jest.advanceTimersByTime}.If you get
actwarnings after updating React, you might have brittle test code that expects an undefined order of execution to be the same on every test run.See https://twitter.com/ph_fritsche/status/1626279438496280576:
I think this may not be resolved for User Event.
the error isnt causing any tests to fail but the only way ive keep the error from bubbling up is to wrap it in an act
Just returning any promise is just masking the issue. It works with pushing it to the next microtask here, but it might fail when you run the same test in a slightly different environment. There you might need to await any other number of additional microtasks, which makes this a randomly working workaround™.
The correct way to fix with is to create a promise that resolves when the state update is applied. Fake timers complicate this a little bit.
I’ve updated the library to version 14.0.0 to fix some act warnings that were being thrown and I’m glad to report that the new release has indeed fixed them.
One shouldn’t need to wrap
@testing-library/user-eventcalls withactas that’s already done for you.I’d start with a simple test case and see if you are following all the requirements since recently there was a recent addition of
setuponuser-eventlibrary, or maybe you are also using an old version of react?I could also be wrong and there might be a bug, but wanted to say that it’s working as expected for me without any “missing act” being logged. It’s important to see a reproducible scenario where this is happening as otherwise it’s possible you are missing something on your tests setup.
This worked for me, we are using yarn and we had to add resolutions instead of overrides.
Thank you @louis-young
I think I managed to extract the issue into reproducible test https://github.com/artursvonda/rtl-14-act-repro. Let me know if this helps. As you see in the test, I’m not doing anything funky.
Thanks for confirming this is not expected. I finally managed to fix the issue on my project by deleting my
package-lock.jsonandnode-modulesand re-runningnpm install.