类型错误:axios.get 不是函数? [英] TypeError: axios.get is not a function?
本文介绍了类型错误: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
};
推荐答案
请看:MDN
正如那里提到的,你需要一个值来收集默认导出
,其余的作为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.
这篇关于类型错误:axios.get 不是函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文