In order to mock my rest call I'm using "axios-mock-adapter".
My service where I'm triggering the call looks like this:
export class ApiService {
private static instance: ApiService
private axiosInstance: AxiosInstance
private constructor() {
this.axiosInstance = axios.create({
baseURL: '...'
headers: {...}
})
}
async getAll(apiRequests: ApiRequest<unknown>[]): Promise<void> {
try {
await Promise.all(apiRequests.map(apiRequest => {
return this.axiosInstance.get<typeof apiRequest.response>(apiRequest.path)
...
Then, in my test:
import MockAdapter from 'axios-mock-adapter'
const mockAdapter = new MockAdapter(axios)
it('should mock', async () => {
// Given
mockAdapter.onGet(/.*/).reply(404)
// WHEN
await expect(ApiService.getInstance().getAll([
new ApiRequest('/test-1'),
new ApiRequest('/test-2')
], false)).rejects.toThrow(Error('Request failed with status code 404'))
})
Here I receive this error:
mockAdapter.onGet is not a function
Any idea how to solve this?