You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
waitFor utils are not wrapped in act so some state updates are not applied to the DOM #1416
test('it loads',async()=>{render(<App/>);awaitwaitForElementToBeRemoved(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.
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.
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.
Activity
MatanBobi commentedon Sep 9, 2025
I think that what you're experiencing is actually this one:
#1214
dyantako commentedon Sep 9, 2025
That scenario doesn't quite match what I'm seeing, my affected tests didn't have any explicit
actcalls 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.dyantako commentedon Sep 9, 2025
This comment here looks relevant #1051 (comment)
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
actwould 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.