提问人:lost9123193 提问时间:8/15/2023 最后编辑:Robin Thomaslost9123193 更新时间:8/19/2023 访问量:59
ERR_UNHANDLED_REJECTION:使用 Typescript 和 Jest 的异步函数错误
ERR_UNHANDLED_REJECTION: Async function error using Typescript and Jest
问:
我有以下代码:
useEffect(() => {
const fetchInfo = async () => {
const config = await fetchAnimal(animalIdFromSite);
if (config) {
setAnimal(config.animal);
}
};
fetchInfo();
}, [animalIdFromSite]);
const fetchAnimal = async (animalId: string): Promise<Configuration | null> => {
try {
const response = await axios.get(
`${process.env.animalSite}/animal/${animalId}`
);
return response.data as Configuration;
} catch (error) {
console.error(`Unable to get configuration: ${error}`);
return null;
}
};
我收到以下错误:
[UnhandledPromiseRejection: This error originated either
by throwing inside of an async function without a
catch block, or by rejecting a promise which was
not handled with .catch(). The promise rejected with
the reason "[object Object]".] {
code: 'ERR_UNHANDLED_REJECTION'
我的测试如下所示:
test("renders content area with display id", () => {
render(
<MemoryRouter initialEntries={["/id/23"]}>
<App />
</MemoryRouter>
);
expect(screen.getByRole("content")).toHaveClass("content-area");
});
如果我删除测试,我的代码就会通过。我很困惑,因为我已经在函数中设置了捕获
答: 暂无答案
评论
fetchInfo().catch(console.error)