React jest 测试未达到 api 覆盖率
我在某些组件中调用了这个 api 代码。
const Api: IApi = {
delete: (url: string): Promise<any> => {
return axios.delete(url);
},
get: (url: string): Promise<any> => {
return axios.get(url)
},
post: (url: string, payload: IContact): Promise<any> => {
return axios.post(url, JSON.stringify(payload))
},
put: (url: string, payload: IContact): Promise<any> => {
return axios.put(url, JSON.stringify(payload))
},
}
export { Api }
除 API 外,所有使用此 api 的组件均已测试为 100% 覆盖率。
没有覆盖的文件是接口(类型)文件。
我现在面临的问题是尝试测试 API,使其达到 100% 的覆盖率,但所有场景都失败了。尽管所有其他方法都已成功模拟,但仅覆盖了 get 方法。
报道如此表现到底是怎么回事?
如何实现此覆盖范围?
I have this api code called in some components.
const Api: IApi = {
delete: (url: string): Promise<any> => {
return axios.delete(url);
},
get: (url: string): Promise<any> => {
return axios.get(url)
},
post: (url: string, payload: IContact): Promise<any> => {
return axios.post(url, JSON.stringify(payload))
},
put: (url: string, payload: IContact): Promise<any> => {
return axios.put(url, JSON.stringify(payload))
},
}
export { Api }
All the components that use this api have been tested to 100% coverage except the API.
The files without coverage are interface (types) files.
The issue I am facing now is to try and test the API so it hits 100% coverage but all scenarios have failed. The get method only is covered even though all the other methods have been successfully mocked.
What is really going on here for the coverage to behave like this?
How can this coverage be achieved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该模拟
axios
库,调用delete
、post
&put
方法到您的测试中并确保正确调用模拟。像这样的事情:
You should mock the
axios
library, call thedelete
,post
&put
methods in your tests and make sure the mocks are correctly called.Something like this: