I am testing my component wit react-testing-library and test works well. I just can't get rid of this warning, fireEvent should by wrapped in act out-of-the-box, but I tried to wrap it again and it did'nt help.
Here is my test case.
it.only("should start file upload if file is added to the field", async () => {
jest.useFakeTimers();
const { getByTestId } = wrapper;
const file = new File(["filefilefile"], "videoFile.mxf");
const fileInput = getByTestId("drop-zone").querySelector(
"input[type='file']"
);
fireEvent.change(fileInput, { target: { files: [file] } });
act(() => {
jest.runAllTimers();
});
await wait(() => {
expect(
initialProps.uploadItemVideoFileConnect.calledWith(file, 123)
).toBe(true);
});
});
Here is the warning
Warning: An update to UploadButtonGridComponent 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 */
fireEvent It's also wrapped in act so you don't need to worry about doing it.
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.
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: act(() => { // render components }); // make assertions.
If you're using a library like React Testing Library, then things like the render function are already wrapped in act() , and you generally will not need to use it explicitly. Note that React Testing Library does have it's own act() function, which is simply a wrapper for ReactTestUtil 's act() .
That issue is caused by many updates inside Component.
I got the same, this is how I solved the issue.
await act( async () => {
fireEvent.change(fileInput, { target: { files: [file] } });
});
In the source code, fireEvent
is already wrapped in act()
.
The problem may be related to this issue, in which async logic (such as a useEffect
) is triggering state changes outside of fireEvent:
https://github.com/kentcdodds/react-testing-library/issues/281
(Without seeing your component implementation it's hard to be sure if this is exactly what's happening in your case.)
Apparently there are plans to include async handling in a future release, so that this won't be a problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With