如何从renderHook获取返回值

时间:2019-12-25 08:22:10

标签: reactjs jestjs react-testing-library

我有一个返回函数的自定义钩子

export function useMyCustomHook = (dispatch) => () => {... some things with dispatching to store };

我正试图在玩笑测试中使用这样的返回值:

  const result = renderHook(() => useMyCustomHook(useDispatch()));
  console.log(result.current);

result.current未定义。那么我怎么才能真正获得返回值呢?

1 个答案:

答案 0 :(得分:0)

根据react-hooks-testing-library renderHook返回带有结果的对象,因此可以访问它:

const { result } = renderHook(() => useMyCustomHook(useDispatch()));
console.log(result.current);