I have axios
with following HttpClient
class
export default class HttpClient {
constructor(baseUrl: string) {
const axiosInstance = axios.create({
validateStatus(status: number) {
return status === 200 || status === 201;
},
});
axiosInstance.interceptors.request.use((config) => {
if (AuthUtil.getAuthHeader()) config.headers = AuthUtil.getAuthHeader();
return config;
});
return new Proxy(this, {
get(_, prop) {
return (url: string, ...args: any) => {
url = baseUrl + url;
return Reflect.get(axiosInstance, prop)(url, ...args);
};
},
});
}
get<T = any, R = AxiosResponse<T>>(_url: string, _config?: AxiosRequestConfig): Promise<R> {
return Promise.resolve(null);
}
.....
}
Here is the service that uses the HttpClient
export default class UserManagementServiceImpl implements UserManagementService {
private client = new HttpClient('/api/user');
async getUser(): Promise<User> {
const res = await this.client.get('/user');
return res.data;
}
I'm testing the service as follows. Here I'm not calling userService.getUser
directly but just for testing I created this.
describe('User actions', () => {
test('creates GET_TERMS_SUCCESS', async () => {
jest.mock('axios', () => {
return {
create: jest.fn().mockReturnValue({
interceptors: {
request: { use: jest.fn(), eject: jest.fn() },
response: { use: jest.fn(), eject: jest.fn() },
},
get: jest.fn().mockReturnValue({ data: user }),
}),
};
});
const user = await userService.getUser();
});
});
// ERROR:
/*
Error: Error: connect ECONNREFUSED 127.0.0.1:80
*/
I have tried multiple other solutions listed here in stack overflow but does not seem to work. How should this be done?