preact-hooks-testing-library icon indicating copy to clipboard operation
preact-hooks-testing-library copied to clipboard

Error in useReducer reduce function is thrown instead of saved into `result.error`

Open mischnic opened this issue 5 years ago • 0 comments

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, dispatch] = React.useReducer((state, action) => {
    throw new Error("X");
  });

  return {
    load() {
      dispatch();
    },
  };
}

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

  act(async () => {
    result.current.load();
  });
  expect(result.error.message).toBe("X");
});

With Preact, this fails because the error is thrown. With React, result.error is populated.

mischnic avatar Sep 09 '20 14:09 mischnic