0

I've written some test that trigger some controlled input and need to wait for a state change.

In this case I supposed to wait for that state change and I tried to use the act function to react to that state change but it looks like the same with or without the act function.

When is act really needed?

2 Answers 2

1

act() docs:

When writing UI tests, tasks like rendering, user events, or data fetching can be considered as “units” of interaction with a user interface. react-dom/test-utils provides a helper called act() that makes sure all updates related to these “units” have been processed and applied to the DOM before you make any assertions

Don't worry, you will know when to use it. You will get a "not wrapped in act(...)" warning like this when you test your component

This is a good post explain when should you need to use it. use cases for manually calling act(...)

Sign up to request clarification or add additional context in comments.

Comments

0

When you perform an action that causes a state update in the component/hook being tested, you should wrap it in the act.

Mostly you can get around this by using waitFor on the assertion. This is when wrapping in act or not won't make any difference apparently.

The recommended way is to wrap it in act and not use waitFor unnecessarily. It will keep your tests quicker and you can avoid making test code asynchronous.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.