react-testing-library icon indicating copy to clipboard operation
react-testing-library copied to clipboard

Bug: global.IS_REACT_ACT_ENVIRONMENT = false does not disable missing act warnings

Open anilanar opened this issue 3 years ago • 9 comments

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

  1. Clone https://github.com/anilanar/react-18-act-env ; which is a create-react-app with an adjustment to App component to trigger an async setState and an adjustment to the test to wait for that async state update.
  2. npm install
  3. npm 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.

anilanar avatar Aug 10 '22 07:08 anilanar

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 avatar Aug 10 '22 11:08 eps1lon

@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

nstepien avatar Aug 11 '22 16:08 nstepien

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

eps1lon avatar Aug 11 '22 18:08 eps1lon

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/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.

and neither of these solutions had any effect on the warning displaying. Any other ideas?

RobRukavina avatar Feb 13 '23 22:02 RobRukavina

@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;
});

anilanar avatar Feb 13 '23 23:02 anilanar

@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;
});

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 😄

RobRukavina avatar Feb 14 '23 01:02 RobRukavina

@anilanar thank you, just tested this solution and it worked! https://github.com/testing-library/react-testing-library/issues/1108#issuecomment-1428830269

RobRukavina avatar Feb 14 '23 17:02 RobRukavina

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 avatar Apr 13 '23 18:04 Lokua

@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.

anilanar avatar Apr 13 '23 18:04 anilanar