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 finishesuser.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.
Read more
- user-event intro (https://testing-library.com/docs/user-event/intro)testing-library.com