Skip to content

Small reproduction of act warnings that are unexpectedly happening after a `fireEvent` call

Notifications You must be signed in to change notification settings

trv-wcaneira/act-warning-fireevent-mcve

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

act-warning-fireevent-mcve

This was originally a repro/MCVE for what I thought was a React/RTL issue with unnecessary/unexplained act warnings. It turned out that I was doing it wrongTM (and learned/was reminded of something in the process).

Background

When we upgraded to React 18 and RTL 13.4, a number of existing tests started to generate act warnings on fireEvent calls; the general flow is:

  1. render a view,
  2. click a button (fireEvent),
  3. assert that a fetch function is called

The issue is that asserting the fetch is not enough, one must wait for the effect to finish and assert on THAT.

How to use this repo

After npm i, use npm t to run the tests in watch mode.

There are two tests you can try, one that generates the warning, and one that does NOT.

  console.error
    Warning: An update to App 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 */

You can see that in the proper test, the effect is allowed to finish completely, and there is no warning.

If you prefer, you can use npm run test:nowatch.

Using npm start will open the test application in a browser to see it visually, if desired.

Warning

The API endpoint this uses will occasionally have CORS errors, when testing in a browser. I wonder if the API owner is load balancing servers and one of them doesn't allow CORS. Simply keep trying if you get such an error.

About

Small reproduction of act warnings that are unexpectedly happening after a `fireEvent` call

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages