【发布时间】:2021-02-12 14:22:45
【问题描述】:
我正在尝试为这个自定义钩子编写一个测试套件。
export const useInitialMount = () => {
const isFirstRender = useRef(true);
// in the very first render the ref is true, but we immediately change it to false.
// so the every renders after will be false.
if (isFirstRender.current) {
isFirstRender.current = false;
return true;
}
return false;
};
非常简单的返回true 或false。
如我所见,我应该使用@testing-library/react-hooks
这是我的尝试:
test("should return true in the very first render and false in the next renders", () => {
const { result } = renderHook(() => {
useInitialMount();
});
expect(result.current).toBe(true);
});
但我得到了undefined,这没有意义,它应该是true 或false。
PS:代码在项目中按预期工作。
【问题讨论】:
标签: reactjs react-hooks react-testing-library react-hooks-testing-library