【发布时间】:2022-02-13 01:38:51
【问题描述】:
我正在JEST 中运行我的第一个单元测试。我安装了Jest Fetch Mock,但仍然收到错误消息“The promise rejected with reason FetchError”。
我做了一些阅读,我想我需要添加一个 async await 或 try catch。有人可以帮我解决这个问题吗?
这是我在useEffect 中制作的fetch。
const [data, setData] = useState(null);
const [isLoading, setLoading] = useState(false);
useEffect(() => {
setLoading(true)
fetch("https://api.github.com/search/repositories?q=created:%3E2017-01-10&sort=stars&order=desc")
.then((res) => res.json())
.then((data) => {
setData(data)
setLoading(false)
})
}, [500]);
【问题讨论】:
标签: javascript reactjs jestjs next.js