I have the following method in a class:
import axios from 'axios'
public async getData() {
const resp = await axios.get(Endpoints.DATA.URL)
return resp.data
}
Then I am trying to set up a Jest test that does this:
jest.mock('axios')
it('make api call to get data', () => {
component.getData()
expect(axios.get).toHaveBeenCalledWith(Endpoints.DATA.URL)
})
The problem is that because I am not mocking the return value, then it gives an error for resp.data
because I'm calling data
on null
or undefined
object. I spent at least 2 hours trying various ways to get this working but I can't find a way such that I can mock axios.get
with some return value.
Jest's documentation uses JavaScript so they give this example axios.get.mockResolvedValue(resp)
but I can't call mockResolvedValue
because that method does not exist on axios.get
in TypeScript.
Also, if you know other good testing library for React other than Jest that does this stuff easily for TypeScript, feel free to share.