TypeError:axios.get不是函数吗? [英] TypeError: axios.get is not a function?
本文介绍了TypeError:axios.get不是函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不确定为什么会出现以下错误:
Not sure why I'm getting the following error:
TypeError: axios.get is not a function
4 |
5 | export const getTotalPayout = async (userId: string) => {
> 6 | const response = await axios.get(`${endpoint}get-total-payout`, { params: userId });
7 | return response.data;
8 | };
9 |
我的服务
import * as axios from 'axios';
const endpoint = '/api/pool/';
export const getTotalPayout = async (userId: string) => {
const response = await axios.get(`${endpoint}get-total-payout`, { params: userId });
return response.data;
};
我的笑话测试:
// import mockAxios from 'axios';
import { getTotalPayout } from './LiquidityPool';
const userId = 'foo';
describe('Pool API', () => {
it('getTotalPayout is called and returns the total_payout for the user', async () => {
// mockAxios.get.mockImplementationOnce(() => {
// Promise.resolve({
// data: {
// total_payout: 100.21,
// },
// });
// });
const response = await getTotalPayout(userId);
console.log('response', response);
});
});
在src/__ mocks __/axios.js中,我有以下内容:
// tslint:disable-next-line:no-empty
const mockNoop = () => new Promise(() => {});
export default {
get: jest.fn(() => Promise.resolve({ data: { total_payout: 100.21 }})),
default: mockNoop,
post: mockNoop,
put: mockNoop,
delete: mockNoop,
patch: mockNoop
};
推荐答案
请查看:从那里开始,您需要一个值来收集default export
,其余的作为X
.在这种情况下,您可以:
As mentoined there, you need a value to collect the default export
and the rest as X
. In this case you could:
import axios, * as others from 'axios';
X在这里是others
.
代替
import * as axios from 'axios';
假设:... from 'axios'
是指您的笑话模拟.让我知道这是否回答了您的问题.
Assumption: ... from 'axios'
is referring to your jest mock. Let me know if that answers your question.
这篇关于TypeError:axios.get不是函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文