`waitFor` utils are not wrapped in `act` so some state updates are not applied to the DOM
Opened this issue · 3 comments
@testing-library/reactversion: 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.
That scenario doesn't quite match what I'm seeing, my affected tests didn't have any explicit act calls before, or changes to timer implementation. The test failure also isn't complaining about missing act, it's just can't find the DOM element it's looking for.
This comment here looks relevant #1051 (comment)
If inside an act scope no state updates, effects etc are flushed. Only at the end. Which is why it wouldn't make sense to wrap a waiting period in act
That makes sense for the waiting period, but the state updates being flushed at the end was behaviour that our tests seem to rely on and its unclear what the correct fix is.
It looks like there was a fix to additionally drain microtasks here #1137. However my understanding is that previously the act would recursively drain microtasks and any state updates, while now it just drains microtasks once. Any state updates from these final microtasks will not be processed.