不确定为什么会出现以下错误:
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
};
import axios from "axios"
- Matt Jaf