【发布时间】:2022-08-18 07:14:03
【问题描述】:
我有一个自定义钩子,它在 mount 上调用 API 并处理状态(isLoading、isError、date、refetch);
钩子很简单:
const useFetch = (endpoint, options) => {
const [data, setData] = useState(null);
const [error, setError] = useState(null);
const [isLoading, setIsLoading] = useState(true);
const [trigger, setTrigger] = useState(true);
const triggerSearch = () => {
setTrigger(!trigger);
};
useEffect(() => {
const fetchData = async () => {
try {
const response = await fetch(
`${process.env.API_URL}${endpoint}`
);
const json = await response.json();
setData(json);
setIsLoading(false);
} catch (error) {
setError(error);
setIsLoading(false);
}
};
fetchData();
}, [endpoint, trigger]);
return {
data,
isLoading,
error,
triggerSearch,
};
};
在尝试测试钩子时,我正在使用 jest 和 testing-library/react。
使用 react 18,不再支持 testing-library 中的 react-hooks,因此我不能使用 renderHook 中的 awaitForNextUpdate,因为它不会返回它。
相反,我们应该使用 act 和 waitFor - 我已经完成并且测试通过了。
问题是我收到以下错误
警告:测试中对 TestComponent 的更新未包含在 行为(...)。
When testing, code that causes React state updates should be wrapped into act(...):test(\"should make an API call on mount\", async () => { const hook = renderHook(() => useFetch(\"/api\")); await act(async () => { await waitFor(() => expect(fetch).toHaveBeenCalledTimes(1)); }); expect(fetch).toHaveBeenCalledTimes(1); expect(getAccessTokenSilently).toHaveBeenCalledTimes(1); expect(hook.result.current.data).toEqual({ message: \"Test\" }); expect(hook.result.current.isLoading).toEqual(false); expect(hook.result.current.error).toEqual(null); });有人可以指出我正确的方向吗?我尝试删除所有断言并仅调用 renderHook,这也会导致相同的错误。
-
嘿,我现在发现了同样的问题,因为我已经更新到 React 18 和最新的 RTL
13.3和 jest5.16.5。您找到任何解决方案了吗?
标签: reactjs react-hooks react-testing-library