Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does react hook throw the act error when used with fetch api?

I keep getting Warning: An update to App inside a test was not wrapped in act(...). in my test suite whenever I make an API request and update the state.

I'm making use of react-testing-library. I also tried using ReactDOM test utils, got the same result. One other thing I tried was wrapping the container in act, still got the same result.

Please note that: My App works and my test passes. I just need to know what I was doing wrong or if it's a bug in the react-dom package that's making that error show up. And it's bad to mock the console error and mute it.

global.fetch = require('jest-fetch-mock');

it('should clear select content item', async () => {
    fetch.mockResponseOnce(JSON.stringify({ results: data }));

    const { container } = render(<App />);

    const content = container.querySelector('.content');

    await wait();

    expect(content.querySelectorAll('.content--item').length).toBe(2);
});

Here's the hook implementation:

const [data, setData] = useState([]);
const [error, setError] = useState('');

const fetchInitData = async () => {
    try {
        const res = await fetch(API_URL);
        const data = await res.json();

        if (data.fault) {
            setError('Rate limit Exceeded');
        } else {
            setData(data.results);
        }
    } catch(e) {
        setError(e.message);
    }
};

useEffect(() => {
    fetchInitData();
}, [isEqual(data)]);
like image 682
Ademola Adegbuyi Avatar asked Feb 18 '19 13:02

Ademola Adegbuyi


3 Answers

For anyone who stumbles upon this more than a year later as I did, the issue Giorgio mentions has since been resolved, and wait has since been replaced with waitFor, as documented here:

https://testing-library.com/docs/dom-testing-library/api-async/

That being the case, I believe the solution to the warning now should be something like this:

import { render, waitFor } from '@testing-library/react';
// ...

it('should clear select content item', async () => {
    fetch.mockResponseOnce(JSON.stringify({ results: data }));

    const { container } = render(<App />);

    const content = container.querySelector('.content');

    await waitFor(() =>
        expect(content.querySelectorAll('.content--item').length).toBe(2);
    );
});

In my case, I had an App component loading data asynchronously in a useEffect hook, and so I was getting this warning on every single test, using beforeEach to render App. This was the specific solution for my case:

  beforeEach(async () => {
    await waitFor(() => render(<App />));
  });
like image 68
Derek Avatar answered Nov 14 '22 15:11

Derek


It's a known problem, check this issue in Github https://github.com/kentcdodds/react-testing-library/issues/281

like image 28
Giorgio Polvara - Gpx Avatar answered Nov 14 '22 15:11

Giorgio Polvara - Gpx


To get rid of the act() warning you need to make sure your promises resolve synchronously. You can read here how to do this.

Summary:

The solution for this is a bit involved:

  • we polyfill Promise globally with an implementation that can resolve promises 'immediately', such as promise
  • transpile your javascript with a custom babel setup like the one in this repo
  • use jest.runAllTimers(); this will also now flush the promise task queue
like image 21
0xR Avatar answered Nov 14 '22 13:11

0xR