I'm learning unit testing in vue with Typescript and I want to test this function:
const getCLients =async (): Promise<Client[]> => {
const {data} = await clientsApi.get('/clients')
return data
}
But I got an error due to the clientsApi. My clients api is an axios instance and looks like this:
import axios from 'axios'
const clientsApi = axios.create({
baseURL:import.meta.env.VITE_API_URL
})
export default clientsApi
My error is this:
TypeError: Cannot read properties of undefined (reading 'get') ❯ getCLients ../src/clients/composables/useClients.ts:14:41
14| const {data} = await clientsApi.get('/clients')
In my tests I have done the axios mock:
vi.mock('axios')
const mockedAxios = axios as jest.Mocked<typeof axios>;
mockedAxios.get.mockResolvedValue({
data: mockClients,
})
I suposed that I had to make a mock of the axios.create but I have tried with a lot of ways but I always get the same type error. I need your help, how can you solve this?