Skip to content

.clear in v14 produces an act warning when used with React hook form.  #938

@mikaelrss

Description

@mikaelrss

Reproduction example

https://github.com/mikaelrss/rhf-rtl-act-bug-repro

Prerequisites

  1. git clone git@github.com:mikaelrss/rhf-rtl-act-bug-repro.git
  2. cd rhf-act-bug-repro
  3. yarn install
  4. yarn test --> See that the tests pass without any act warnings.
  5. git checkout user-event@14
  6. yarn install
  7. yarn test --> See that the tests pass but now with an act warning.

Expected behavior

I expect that the test below passes without any act warnings.

it("should show an error message when title field is cleared", async () => {
  render(<Form />);

  expect(screen.queryByText("Title is required")).not.toBeInTheDocument();

  await userEvent.clear(screen.getByLabelText("Title"));

  expect(await screen.findByText("Title is required")).toBeInTheDocument();
});

Actual behavior

The following test produces an act warning.

it("should show an error message when title field is cleared", async () => {
  render(<Form />);

  expect(screen.queryByText("Title is required")).not.toBeInTheDocument();

  await userEvent.clear(screen.getByLabelText("Title"));

  expect(await screen.findByText("Title is required")).toBeInTheDocument();
});

Wrapping the await userEvent.clear(screen.getByLabelText("Title")) in an act removes the warning.

it("should show an error message when title field is cleared", async () => {
  render(<Form />);

  expect(screen.queryByText("Title is required")).not.toBeInTheDocument();

  await act(async () => {
    await userEvent.clear(screen.getByLabelText("Title"));
  });

  expect(await screen.findByText("Title is required")).toBeInTheDocument();
});

This works as expected with @testing-library/user-event@13.2.1 but breaks in @testing-library/user-event@14.1.1

User-event version

14.1.1

Environment

Testing Library framework: @testing-library/react@13.0.0

JS framework: react@18.0.0 and react-hook-form@7.30.0

Test environment: react-scripts@5.0.1 which uses jest@27.5.1

Additional context

Working version on main branch of repro is @testing-library/user-event@13.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingreleased

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions