-
Notifications
You must be signed in to change notification settings - Fork 252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
userEvent.click triggers warning that it must be wrapped in act in beforeEach #497
Comments
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! |
Can you update the documentation for userEvent.click to say either: Right now the docs say that the only time you need to wrap it is when you are using delay, and this example shows that is simply not true. |
You're more than welcome to make a PR for recommendation
This is incorrect. |
I am reading through them but struggling to find the relevant line of code that explains the functional difference. When I do:
I do not get a warning. But if move that same line into a beforeEach, I do. |
So it sounds like I need to open a PR that says, "You do not need to await Correct? |
The recommendation should be to not use this in |
Whether or not to use Users do not expect there to be a functional difference between putting something in Your posts make a compelling stylistic case for "never use before/after" in general, but there's nothing I see in these posts that claims there is a functional difference. Yet, there seems to be some functional difference with I plan to open a PR that says, "Stylistically, we do not believe in using before/after blocks. See [blog posts about stylistic reasons]. Functionally, if you do choose to use a before/after block, you must |
This is also incorrect. As your example demonstrates it makes a semantic difference. You can feel free to open a PR like that, but it will not be merged if it doesn't communicate our intended recommendation. |
Feel free to change the language of the PR as needed. |
Yes, my example does illustrate there is some kind of functional difference. I didn't see anything in your blog posts that highlighted the functional difference that is relevant to the behavior in this case. So I still don't get why, even after reading all three posts. If you can point me to where in the blog post, you talk about that functional difference, I will more clearly reference or summarize that difference in the PR. |
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 :) |
Sure, sounds good. 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. :-) |
While I don't see good reason to use I guess your test setup - if performed in specific order - covers up some delayed state change. |
I am very struggle with 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 so the alternative way is using writing test for front end is really difficult compare to backend.. |
One use case would be to test the separate parts of the UI after a button press. You could also copy & paste code for each test and wait for it to finish, but you could say that for almost any use case for beforeEach. I believe the intuitive name implies it should be used for code that needs to be parsed before every test. I'm not clear on why it is bad practice to fire events in If an explanation could be given, or give an option for user event to be waited on within the built-in 'act', I think that would be helpful. In terms of UX principle, if multiple developers are using a function 'wrong' than the design should be reconsidered, not blaming the user. |
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 There is no need to copy&paste code. Write a
This recommendation is of course biased. There is no technical barrier preventing you from writing you whole test in a If you have further questions: |
I just tried using
This is the simplest line I can possible imagine for changing state yet jest asks me to wrap it. const user = setup()
render(<Component />)
const component = screen.get...
await user.click(component) No idea what's going on. |
Hello, I believe the const component = await screen.findBy... |
I meant |
Ive noticed in my tests the exact same thing, and its ever since I added
seems very verbose, i'm also confused by this, but it seems to work.... |
I've run into the same thing as well, and had to do the same thing to get rid of the warnings. |
Do not just add act() calls until React shuts up! https://ph-fritsche.github.io/blog/post/react-act 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. |
Did anyone figure out why this works:
and this does not:
|
seems like you have a different versions of @testing-library/dom package in project, take a look on this discussion, it helped me |
Thank you, it fixed my issue |
I have no idea why this is flaking. There are warnings about things not being wrapped in act() which may be relevant... this makes the warnings happy, although apparently should not be necessary. testing-library/user-event#906 and testing-library/user-event#497 are depressing reading (making the versions the same didn't help). I think my conclusion might be to do this until we're able to upgrade to the latest testing-library, then re-evaluate. It still may or may not fix the flake.
I have no idea why this is flaking. There are warnings about things not being wrapped in act() which may be relevant... this makes the warnings happy, although apparently should not be necessary. testing-library/user-event#906 and testing-library/user-event#497 are depressing reading (making the versions the same didn't help). I think my conclusion might be to do this until we're able to upgrade to the latest testing-library, then re-evaluate. It still may or may not fix the flake.
I have no idea why this is flaking. There are warnings about things not being wrapped in act() which may be relevant... this makes the warnings happy, although apparently should not be necessary. testing-library/user-event#906 and testing-library/user-event#497 are depressing reading (making the versions the same didn't help). I think my conclusion might be to do this until we're able to upgrade to the latest testing-library, then re-evaluate. It still may or may not fix the flake.
@testing-library/user-event
version: 12.2.2Relevant code or config
What you did:
Ran the test
What happened:
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.
The text was updated successfully, but these errors were encountered: