Skip to content

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

Closed
@mikaelrss

Description

@mikaelrss

Reproduction example

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

Prerequisites

  1. git clone [email protected]: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/[email protected] but breaks in @testing-library/[email protected]

User-event version

14.1.1

Environment

Testing Library framework: @testing-library/[email protected]

JS framework: [email protected] and [email protected]

Test environment: [email protected] which uses [email protected]

Additional context

Working version on main branch of repro is @testing-library/[email protected]

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