Bug: global.IS_REACT_ACT_ENVIRONMENT = false does not disable missing act warnings
This issue was first created in facebook/react, see https://github.com/facebook/react/issues/24854.
The rational behind why we want to disable act warnings: https://github.com/facebook/react/issues/23197
Potentially related to using jest. Didn't confirm.
React version: 18.2.0
Steps To Reproduce
- Clone https://github.com/anilanar/react-18-act-env ; which is a
create-react-appwith an adjustment toAppcomponent to trigger an asyncsetStateand an adjustment to the test to wait for that async state update. npm installnpm test -- --watchAll
The current behavior
It logs Warning: An update to App inside a test was not wrapped in act(...).
The expected behavior
It does not log that warning.
We enable missing act warnings in the global setup that comes with @testing-library/react. To disable the global setup, import from @testing-library/react/pure instead. Alternatively, try setting global.IS_REACT_ACT_ENVIRONMENT = false; in a beforeAll instead which is where all side-effects inside tests should go anyway.
Hope this helps. If there's anything unclear, please let me know.
@eps1lon any comment on the following? Why does it set IS_REACT_ACT_ENVIRONMENT to false in async wrappers?
https://github.com/testing-library/react-testing-library/issues/1051#issuecomment-1206927212
Why does it set IS_REACT_ACT_ENVIRONMENT to false in async wrappers?
Because we're not acting when we wait for something to flush. Anything wrapped in act will only flush updates when we exit its scope. For example, if we would wait asynchronously for 5s which is wrapped in act then no state update would be flushed during that period defeating the purpose of waitFor
More detailed explainer: https://github.com/testing-library/react-testing-library/pull/937#discussion_r706618576
I am having this issue also.
We enable missing act warnings in the global setup that comes with
@testing-library/react. To disable the global setup, import from@testing-library/react/pureinstead. Alternatively, try settingglobal.IS_REACT_ACT_ENVIRONMENT = false;in abeforeAllinstead which is where all side-effects inside tests should go anyway.Hope this helps. If there's anything unclear, please let me know.
and neither of these solutions had any effect on the warning displaying. Any other ideas?
@RobRukavina
An idea:
For global.IS_REACT_ACT_ENVIRONMENT = false;, it can be a beforeAll ordering issue. Testing library uses beforeAll too so you need to make sure your beforeAll runs after testing library's.
You can make sure your beforeAll runs later by importing testing library. e.g.
// make sure you import it here
import '@testing-library/react';
beforeAll(() => {
global.IS_REACT_ACT_ENVIRONMENT = false;
});
@RobRukavina
An idea:
For
global.IS_REACT_ACT_ENVIRONMENT = false;, it can be abeforeAllordering issue. Testing library usesbeforeAlltoo so you need to make sure yourbeforeAllruns after testing library's.You can make sure your
beforeAllruns later by importing testing library. e.g.// make sure you import it here import '@testing-library/react'; beforeAll(() => { global.IS_REACT_ACT_ENVIRONMENT = false; });
Thank you. I have changed the test so that I don't need to do this currently, but if I end up having to revert I will try this 😄
@anilanar thank you, just tested this solution and it worked! https://github.com/testing-library/react-testing-library/issues/1108#issuecomment-1428830269
The idea above does not work for me. Also, setting global.IS_REACT_ACT_ENVIRONMENT = false results in Warning: The current testing environment is not configured to support act(...), however my tests are still passing.
@Lokua Because you are not supposed to use act when IS_REACT_ACT_ENVIRONMENT is falsy. That warning is logged when you use act and react detects that IS_REACT_ACT_ENVIRONMENT is not true.