Tests failures in React 19's concurrent mode #1800
-
We’re working on a project where approximately 600 tests are failing when React 19's Concurrent Mode ( I understand how to fix them — using fireEvent, userEvent, or wrapping with act — but I'm wondering: Example 1 (missing async act) await store.setPayPeriodDetails({ startDate: 'startDate', endDate: 'endDate', currentPage: 1 });
expect(store.state.transactionsData).toMatchSnapshot('transactionsData');
expect(OrderHistoryService.fetchPayPeriodDetails).toHaveBeenCalledTimes(1); would need to be updated to await act(async () => {
await store.setPayPeriodDetails({ startDate: 'startDate', endDate: 'endDate', currentPage: 1 });
});
expect(store.state.transactionsData).toMatchSnapshot('transactionsData');
expect(OrderHistoryService.fetchPayPeriodDetails).toHaveBeenCalledTimes(1); Example 2 (missing act) wrapper.getByTestId(CHAT_MESSAGE.VIDEO).props.onError();
expect(wrapper.getByTestId(CHAT_MESSAGE.UNSUPPORTED_FILE_ERROR).props).toMatchSnapshot(); would need to be updated to act(() => {
wrapper.getByTestId(CHAT_MESSAGE.VIDEO).props.onError();
});
expect(wrapper.getByTestId(CHAT_MESSAGE.UNSUPPORTED_FILE_ERROR).props).toMatchSnapshot(); Example 3 (missing fireEvent/userEvent) wrapper.getByTestId(CHAT_MODAL.TEXT_INPUT).props.onChangeText('text or something else');
await fireEvent.press(wrapper.getByTestId(CHAT_MODAL.SCAN_ICON));
expect(wrapper.getByTestId(CHAT_MODAL.TEXT_INPUT).props.value).toEqual('text or something else'); would need to be updated to await user.type(wrapper.getByTestId(CHAT_MODAL.TEXT_INPUT), 'text or something else'); // usage of user.type or fireEvent.changeText
fireEvent.press(wrapper.getByTestId(CHAT_MODAL.SCAN_ICON));
expect(wrapper.getByTestId(CHAT_MODAL.TEXT_INPUT)).toHaveDisplayValue('text or something else'); Example 4 (missing fireEvent/userEvent) input.props.onChangeText('5');
input.props.onEndEditing();
expect(input.props.value).toEqual('5.0'); would need to be updated to: await user.paste(input, '5'); // usage of user.paste or fireEvent
expect(input).toHaveDisplayValue('5.0'); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I am working on better concurrent mode support in #1788 but that concerns mainly tests with Regarding your tests, even in React 18, you should wrap your code that triggers re-renders in When you use relevant User Event interactions, as that gives you much more realistic testing, as User Event emits sequence of multiple event for each interaction, e.g. |
Beta Was this translation helpful? Give feedback.
-
@mdjastrzebski Why does RNTL use sync |
Beta Was this translation helpful? Give feedback.
I am working on better concurrent mode support in #1788 but that concerns mainly tests with
Suspense
element.Regarding your tests, even in React 18, you should wrap your code that triggers re-renders in
act
, as it allows React to run sync effects. With React 19 that changes toawait act(async () => { ... })
due to concurrent mode.When you use relevant User Event interactions, as that gives you much more realistic testing, as User Event emits sequence of multiple event for each interaction, e.g.
onPressIn
,onPress
,onPressOut
forpress
and fortype
it simulates user typing letter by letter with numerous events triggered for each letter.