score:8

Accepted answer

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

score:-1

I had this problem and gave up using wait and async instead used jest faketimers and so on, so your code should be something like this.

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

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

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

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

    // await wait();
    act(() => {
      jest.runAllTimers();
    });

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

score:2

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

score:4

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

Related Query

More Query from same tag