如何在模糊时测试 formik yup 验证错误?

How to test formik yup validation error on blur?

我写了下面的测试:

it('validates the first name cannot be blank', () => {
    const { findByLabelText, getByText } = render(<Profile />);
    const firstName = findByLabelText('first name');
    firstName.value = '';
    fireEvent.blur(firstName);
    const error = getByText('First name is required');
    expect(error).not.toBeNull();
  });

测试运行后出现错误:

Unable to find the "window" object for the given node.

我怎样才能通过这个测试?

事实证明我错误地设置了名字的值。事实上,在这种情况下不需要设置名字,因为它默认为 ''。正确的测试实现是这样的:

it('validates the first name cannot be blank', async () => {
const { getByLabelText, getByText } = render(<Profile />);
const firstName = getByLabelText(/first name/i);

fireEvent.blur(firstName);

let error;
await waitFor(() => {
  error = getByText('First name is required');
});

expect(error).not.toBeNull();

});