Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
62
rated 0 times [  65] [ 3]  / answers: 1 / hits: 64455  / 6 Years ago, wed, september 19, 2018, 12:00:00

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 |


My service:



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;
};


My jest test:



// 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);
});
});


In the src/__mocks__/axios.js I have this:



// 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
};

More From » typescript

 Answers
8

Please look at: MDN


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 being others here.


Instead of


import * as axios from 'axios';

Assumption: ... from 'axios' is referring to your jest mock.


[#53462] Friday, September 14, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gregorio

Total Points: 362
Total Questions: 95
Total Answers: 93

Location: Puerto Rico
Member since Sun, Jun 27, 2021
3 Years ago
gregorio questions
Fri, Apr 8, 22, 00:00, 2 Years ago
Mon, Sep 6, 21, 00:00, 3 Years ago
Sun, Sep 13, 20, 00:00, 4 Years ago
;