React Native 测试库显示使用 `waitFor` 的警告

React Native Testing Library shows a warning for using `waitFor`

我用 Jest 和 react-native-testing-library 进行了这个测试

it('submits form on submit button press', async () => {
  const onSubmit = jest.fn();
  const values = { username: 'jack' };
  const { getByRole } = render(
    <Form initialValues={values} onSubmit={onSubmit} />,
  );

  const button = getByRole('button');

  fireEvent.press(button);

  await waitFor(() => {
    expect(onSubmit).toBeCalledWith(values, expect.anything());
  });
});

它通过了,但是它抛出了这个警告

Warning: You seem to have overlapping act() calls, this is not supported. Be sure to await previous act() calls before making a new one.

注意:如果我删除 waitFor 警告消失但测试失败,因为它没有立即调用 onSubmit 函数。

将 Jest 预设从 "react-native" 更改为 "@testing-library/react-native" 解决了问题。

努力改变

await waitFor(() => {
    expect(onSubmit).toBeCalledWith(values, expect.anything());
});

waitFor(() => {
    expect(onSubmit).toBeCalledWith(values, expect.anything());
});