CodePark/ questions / User interactions
START PRACTICINGPRACTICE
Frontend question · Testing › User interactions

Why does this test fail to find 'Hello, Ana'?

test("shows greeting", async () => {
  const user = userEvent.setup();
  render(<Greeting />);
  user.type(screen.getByLabelText(/name/i), "Ana");
  expect(screen.getByText("Hello, Ana")).toBeInTheDocument();
});
user.type returns a promise and isn't awaited, so the assertion runs before typing finishes

user.type returns a promise, and the test doesn't await it. Every user-event v14 interaction is async. Without await, the assertion runs before the keystrokes are processed. The other options are wrong. getByLabelText does return the input linked to the label. And calling setup() before render is the recommended order.