Skip to content

waitFor utils are not wrapped in act so some state updates are not applied to the DOM #1416

@dyantako

Description

@dyantako
  • @testing-library/react version: 16.3.0
  • Testing Framework and version: jest 30.1.3
  • DOM Environment: jsdom 26.1.0

Relevant code or config:

  test('it loads', async () => {
    render(<App />);

    await waitForElementToBeRemoved(screen.queryByRole('progressbar'));
    // Commenting this out fails the test
    // await act(() => new Promise((r) => setTimeout(r, 0)));

    expect(screen.getByText('Loaded')).toBeDefined();
  });

What you did:

Upgrading to React 18 along with react testing library v14, I found that some of my tests were now failing unless I add an explicit async act.

Digging into it, it seems to be this change to the async wrapper passed to the dom testing library where it no longer directly calls act, so state updates from the final iteration of the waitFor are not flushed to the DOM.

It looks like it only flushes the microtask queue, which won't give react an opportunity to process this state update.

Reproduction:

I'm using React 18 and RTL 14 but I've reproduced it on React 19 and RTL 16.

https://stackblitz.com/edit/rtl-template-4a3jt9jx?file=src%2FApp.test.tsx&view=editor

Suggested solution:

I don't have enough knowledge of the codebase to understand why the call to act was removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions