我用Jest和react本机测试库进行了这个测试
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());
});