Skip to content

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

Open
@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, 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.

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