user-event: userEvent.click triggers warning that it must be wrapped in act in beforeEach
@testing-library/user-eventversion: 12.2.2
- Testing Framework and version: Jest 26.6.3
- DOM Environment: whatever default ships with Jest 26.6.3
Relevant code or config
beforeEach(() => {
userEvent.click(button.getByTestId('foobar'));
});
What you did: Ran the test
What happened:
Warning: An update to Foobar inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the output */
This ensures that you're testing the behavior the user would see in the browser. Learn more at https://fb.me/react-wrap-tests-with-act
in Foobar
Reproduction repository:
Problem description: We don’t have this problem when clicking the same button inside the test itself. But in beforeEach, it flips out and demands that we wrap in act and await. Then the warning goes away.
Suggested solution: Either fix it or document it in the description for userEvent.click.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 25 (15 by maintainers)
seems like you have a different versions of @testing-library/dom package in project, take a look on this discussion, it helped me
I’m afraid that I don’t have any more time to explain it now, but if you’d like to come to my office hours sometime I could walk you through it then 😃
Did anyone figure out why this works:
and this does not:
Sure, sounds good. I read the post again and still didn’t see anything there that explains the difference for this case.
I’ll come to office hours and then maybe we can add a sentence of explanation to either the blog post, the README, or this issue. So it won’t just say “don’t use because style”, it says “don’t use because functional reason”.
That way a future dev has a fighting chance at discovering the why and you don’t have to explain it to anyone else. 😃
Ive noticed in my tests the exact same thing, and its ever since I added
awaitto theuserEvent...line as the test is now async based on some other factors. Now after adding theawaitbefore theuserEvent, ive had to wrap in act -seems very verbose, i’m also confused by this, but it seems to work…
I am very struggle with
actwarning when using userEvent@14 for few days, I tried to understand the best practice of writing good test suits for react app… but due to breaking changes on userEvent@14, I had to change to async await method when usinguserEvent.click()or etc… that was my first struggle I had when follow others answer at stack overflow which already outdated…I had read this blog https://kentcdodds.com/blog/fix-the-not-wrapped-in-act-warning but because of my project is using the mui packages… I had lost my code control to fix the bug…
The error that I get was
An update to ForwardRef(FormControl) inside a test was not wrapped in act(...).… I had double check my code… clearly is from mui side…so the alternative way is using
fireEvent,then the most confuse things is that whyfireEventno warning at all butuserEventdo have warning foract… base on my research. both of them should wrapped withact…writing test for front end is really difficult compare to backend…
Do not just add act() calls until React shuts up! https://ph-fritsche.github.io/blog/post/react-act ☝️ This article explains why adding
actis usually not the correct fix when there is anactwarning.Either your code has an unexpected state update or your setup isn’t correct.
As mentioned earlier this issue already contains outdated and unrelated information. Please use the Q&A section or join our Discord server if you need help setting up your test environment or writing tests.
user-event@12.2. In the latest (and currently only supported version)user-event@14all methods are asynchronous.user-eventshould not be wrapped inact. If you get anactwarning, your code does something it either shouldn’t or that you are not aware of - like a delayed asynchronous state update.An interaction that involves moving parts of the component under test should never be before the test. A failure of this step is a failure of the component under test.
And those cases not covered by “almost” should go into a
beforeEach. Rule of thumb: If there’s a symmetricalafterEach/afterAll, the code should go into abeforehook. If there isn’t, it is a mistake most of the time.There is no need to copy&paste code. Write a
renderThisPartOfMyAppfunction and invoke it in the first line of the test. The next dev who has to work with the test might thank you, because they can follow the function name to its declaration and don’t need to jump through code to findbeforeEachhooks that might or might not be there.This recommendation is of course biased. There is no technical barrier preventing you from writing you whole test in a
beforeEachhook. It’s just that each line in abeforehook has the potential to obfuscate a problem or - like this issue demonstrates only too well - to make people look for problems where there is none. That’s why we recommend to avoid this.If you have further questions: Please, let’s keep this issue board tidy and use it for discussing technical details on definite issues. We’ve got a Discussions board with Q&A section and a Discord server for everything else.
While I don’t see good reason to use
userEventinside ofbeforeEach, it certainly is possible and does not require some extraactper se: https://codesandbox.io/s/quiet-fast-clsk9?file=/src/App.test.jsI guess your test setup - if performed in specific order - covers up some delayed state change. You might want to examine what exactly makes your code run synchronously / in specific order in one case and asynchronously in the other. Maybe a mocked
window.setTimeout?You’re more than welcome to make a PR for recommendation
a.This is incorrect.
userEvent.clickis already wrapped in act. The reason the warning is going away fro you isn’t because ofactbut because ofasync/await. If you read the blog posts I provided hopefully they will explain why.Hi @amandapouget,
Here’s a blog post to explain what that warning is all about: https://kentcdodds.com/blog/fix-the-not-wrapped-in-act-warning
And here’s another blog post to suggest you change the structure of your test to avoid issues like this: https://kentcdodds.com/blog/avoid-nesting-when-youre-testing
This may help as well: https://kentcdodds.com/blog/write-fewer-longer-tests
The recommendation is to restructure your test and not do stuff like this in those setup/teardown hooks (like beforeEach and afterEach). Good luck!