Skip to content

waitForNextUpdate times out with async function #4

@mischnic

Description

@mischnic
import React from "preact/compat";
import { act, renderHook } from "@testing-library/preact-hooks";

// works:
// import React from "react";
// import { act, renderHook } from "@testing-library/react-hooks";

function useTest() {
  let [state, setState] = React.useState(1);

  return {
    value: state,
    async increment() {
      await new Promise((res) => setTimeout(res, 100));
      setState((i) => i + 1);
    },
  };
}

test("test", async () => {
  let { result, waitForNextUpdate } = renderHook(() => useTest());

  expect(result.current.value).toBe(1);
  await act(async () => {
    result.current.increment();
    await waitForNextUpdate();
  });
  expect(result.current.value).toBe(2);
});

Without await waitForNextUpdate(), the second assertion fails.
With that call, Jest times out (this works as expected when using React).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions