Code Monkey home page Code Monkey logo

act-warning-mcve's Introduction

act-warning-mcve

This was a quick MCVE (Minimal, Complete, Verifiable example) to demonstrate what I think could be a bug/unexpected behavior in either React 18 or RTL 13.4. OTOH, it could just be that I'm doing it wrongTM. :-)

Background

Ever seen this testing React apps? :)

  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 */
    
    This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act

From what little I understand, the infamous "act warning" can occur for a couple different reasons, such as state changes being triggered by async updates outside React's call stack, or because a test ends before React is done updating. This article is also a decent read.

In the past, we have avoided act warnings in our application because we use the react-testing-library, whose functions apply the necessary act function calls around event and render functions, AND help provide async utilities to ensure the test waits for all renders to complete before exiting.

When we upgraded to React 18 and RTL 13.4, a number of existing tests started to throw "act" warnings for the first time. These tests had a common pattern of testing code with a fetch call and 'Loading...' indicator showing until the fetch settled.

Through careful trial and error, I identified that an awaited findByText('Loading...') call was causing the warning, though I'm not sure why. If I change the code to simply not wait, e.g. queryByText('Loading...') no warnings are generated.

I plan to open an issue (question) with the RTL maintainers so I understand why we see this behavior.

How to use this repo

After npm i, use npm t to run the tests and see the warning (or use npm run test:nowatch if you prefer). There are two tests you can try, one that generates the warning, and one that does NOT. The only difference is the use of async utilities like findByText or waitFor on the "Loading..." text. Feel free to skip individual tests to see the difference in warning output.

Using npm start will open the test application in a browser to see it visually, if desired. Throttle the network speed in your devtools to see "Loading..." if needed.

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.

act-warning-mcve's People

Contributors

trv-wcaneira avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.