Skip to content
Advertisement

Preventing “not wrapped in act(…)” Jest warning when state update doesn’t affect UI

I’m trying to figure out if there is a way to prevent the “not wrapped in act(…)” warning thrown by Jest/testing-library when I have nothing to assert after the state update that causes the warning happens, or if I should just ignore this warning.

Suppose I have this simple component:

JavaScript

Suppose I want to simply test that this component renders okay even if getData() doesn’t return any data for me.

So I have a test like this:

JavaScript

This test will pass, but I’ll get the “not wrapped in act(…)” warning because the test will finish before getData() has a chance to finish.

In this case, the response from getData() sets arr to the same value (an empty array) as I have initially set it to at the top of the component. As such, my UI doesn’t change after the async function completes—I’m still just looking at a paragraph that says “no array items”—so I don’t really have anything I can assert that would wait for the state update to complete.

I can expect(getData).toHaveBeenCalledTimes(1), but that doesn’t wait for the state to actually be updated after the function call.

I have attempted an arbitrary pause in the test to allow time for setArr(items) to happen:

JavaScript

But that doesn’t seem to help, and I’m honestly not sure why.

Is there a way to handle this situation by modifying only the test?

I am sure I could fix the problem by refactoring MyComponent, e.g., by passing arr to MyComponent as a prop and moving the getData() call to a parent component, or creating some custom prop used only for testing that would skip the getData() call altogether, but I don’t want to be modifying components purely to avoid warnings in tests.

I am using testing-library/react, v11.2.2.

Advertisement

Answer

You can use findByText (a combination of getByText and waitFor) to ensure all updates have happened when the assertion resolves.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement