【发布时间】:2021-03-28 23:16:43
【问题描述】:
我有一个配置 axios 的模块:
// config/axiosConfig.js
import axios from 'axios';
const instance = axios.create({
baseURL: 'http://localhost:8080/api/v1'
});
export default instance;
还有一个使用它来进行 api 调用的模块:
// store/actions.ts
import axiosInstance from 'config/axiosConfig';
export const fetchUsers = () => (dispatch: ThunkDispatch<{}, {}, AnyAction>) => {
dispatch(loading(true));
return axiosInstance.get('/users')
.then(res => {
dispatch(loading(false));
dispatch(fetched(res.data));
})
.catch(err => dispatch(error(true)));
}
...
我想模拟 axios 配置文件来测试我的 api 文件。我尝试了很多很多方法,但没有任何效果。我以为它会很简单
// store/actions.test.ts
import axiosInstance from 'config/axiosConfig';
jest.mock('config/axiosConfig');
axiosConfig.get.mockResolvedValue({users: mockUserList});
...
但我想这不是它的工作原理。
编辑:当我将axiosConfig.get.mockResolvedValue({users: mockUserList}); 放在测试中时,我的问题中的方法有效,而不是在模拟调用下。
【问题讨论】:
-
看起来你做对了,但我需要看看你的完整测试
标签: javascript typescript axios jestjs