waitFor()

Waits for asynchronous assertions to pass. Use when testing code that updates state after async operations.

Syntax

testing
await waitFor(() => expect(element).toBeVisible())

Example

testing
test("loads user data", async () => {
  render(<UserProfile userId="123" />);

  expect(screen.getByText("Loading...")).toBeInTheDocument();

  await waitFor(() => {
    expect(screen.getByText("Alice Johnson")).toBeInTheDocument();
  });

  expect(screen.queryByText("Loading...")).not.toBeInTheDocument();
});