Javascript 使用SWR使用多个获取请求更新未包装在act()中

Javascript 使用SWR使用多个获取请求更新未包装在act()中,javascript,testing,jestjs,react-testing-library,swr,Javascript,Testing,Jestjs,React Testing Library,Swr,我有一个测试,如果/endpoint1失败,应该显示一条错误消息。测试通过,但我从React测试库中得到以下错误: Warning: An update to HomePage inside a test was not wrapped in act(...). 为了清楚起见,这是狙击手。你可以看到我正在使用发送请求。我正在使用模拟请求 导出默认函数App(){ const{data:data1,error:error1}=useSwr(“/endpoint1”) const{data:dat

我有一个测试,如果
/endpoint1
失败,应该显示一条错误消息。测试通过,但我从React测试库中得到以下错误:

Warning: An update to HomePage inside a test was not wrapped in act(...).
为了清楚起见,这是狙击手。你可以看到我正在使用发送请求。我正在使用模拟请求

导出默认函数App(){
const{data:data1,error:error1}=useSwr(“/endpoint1”)
const{data:data2,error:error2}=useSwr(“/endpoint2”)
如果(错误1 | |错误2){
返回
}
返回
}
出现此错误是因为
/endpoint2
/endpoint1
之后解析,导致组件重新加载。我相信一旦第一个请求出现错误就取消第二个请求会起作用。我对使用了
AbortController()
useSwr()
做了一个包装,并公开了一个
cancel()
函数,但是这个代码段也会出现同样的错误:

导出默认函数App(){
const{data:data1,error:error1,cancel:cancel1}=useCancelableSWR(
“/endpoint1”
)
const{data:data2,error:error2,cancel:cancel2}=useCancelableSWR(
“/endpoint2”
)
如果(错误1 | |错误2){
如果(!data1 | |!error1)取消2()
如果(!data2 | |!error2)cancel1()
返回错误

} 返回成功

}
出于某种原因,即使在取消一个端点之后,它仍然会解析并导致重新渲染。我是不是遗漏了什么?谢谢

编辑:

以下是失败的测试:

test(“如果无法获取/endpoint1,则显示错误”),async()=>{
mockServer.use(
rest.get(“/endpoint1”,(\ux,res,ctx)=>res(ctx.status(500)))
)
mockServer.use(
rest.get(“/endpoint2”,(\ux,res,ctx)=>res(ctx.status(200)))
)
render()
const error=wait screen.findByText(“错误”)
expect(error).toBeInTheDocument()
})
和我的自定义抓取函数:

异步函数serverFetch(端点){ const response=等待获取(“http://localhost:8080“+端点) const body=response.json() 如果(!response.ok){ 抛出新错误(“提取数据时出错。”) } 返回体 }
您是否也可以为失败的测试添加代码?为什么
useSWR
没有
fetcher
功能?您是使用原始的
useSWR
hook还是自定义版本的
useSWR
?@juliomalves我刚刚更新了问题,添加了失败的代码test@slideshowp2我用的是一个自定义抓取程序,我已将其添加到上述问题中您在
serverFetch
函数中缺少
await
const body=await response.json()