admin管理员组文章数量:1431002
I am implementing VueQuery in my application. Now I am trying to write unit tests for this. The success case works fine, but when I mock an error case it seems to error is not being handled by VueQuery.
I am passing an Axios method to my useQuery instance. The in my unit test I mock my API client as a spy and mock the rejected value while passing an AxiosError object.
This is my unit test:
it('show an error message when an error response is returned', async () => {
const spy = vi.spyOn(PersonApi.prototype, 'createPerson');
spy.mockRejectedValueOnce({
isAxiosError: true,
toJSON: () => ({}),
name: 'AxiosError',
message: 'Request failed with status code 400',
response: {
data: {message: 'Error'},
status: 400,
statusText: 'Bad Request',
headers: {},
config: {headers: {} as AxiosRequestHeaders},
request: {},
},
config: {headers: {} as AxiosRequestHeaders}
} as AxiosError);
wrapper = createComponent();
await flushPromises();
expect(spy).toHaveBeenCalledOnce();
expect(wrapper.find('#error-status-message').exists()).toBe(true);
});
And this is the useQuery instance:
const {isLoading, mutateAsync, isError} = useMutation({
mutationKey: ['createPerson'],
mutationFn: async () => await personApi.createPerson({
id,
})
});
When I run the test I get a message in the Run console of 'Request failed with status code 400', which is technically correct but it seems the entire test is failing because the exception is not being handled. Anyone has an idea?
本文标签: typescriptHow to mock Axios error response when using VueQueryStack Overflow
版权声明:本文标题:typescript - How to mock Axios error response when using VueQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745570283a2664006.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论