I have this all over my application:
I have a well covered function, however it says "branch not covered" on async function part in test coverage report generated by Istanbul after running Jest test coverage script.
What does this mean? How can I cover it?
I don't understand what is not covered in these functions.
Test example for "getPolicyStatsAPI" function: The rest of the functions are tested the same way. I don't believe the answer is in the test suites. But if needed I can share the rest.
describe(`API Calls`, () => {
const mockFetch = jest.fn()
global.fetch = mockFetch
beforeEach(() => {
mockFetch.mockClear()
})
test('getPolicyStatsAPI fetches the data and returns it as expected', async () => {
mockFetch.mockReturnValueOnce(createAPIResponse(policySearchState))
const response = await getPolicyStatsAPI(reqBody)
expect(fetch).toBeCalledTimes(1)
expect(response).toEqual(policySearchState.data)
})
test('getPolicyStatsAPI returns error on exception', async () => {
const error = new Error(errorMessage)
// eslint-disable-next-line prefer-promise-reject-errors
mockFetch.mockImplementationOnce(() => Promise.reject(errorMessage))
await expect(getPolicyStatsAPI(reqBody)).rejects.toEqual(error)
expect(fetch).toBeCalledTimes(1)
})
})
Here is the code I get "branch not covered" warning on: Branch not covered warning appears right over "async function" part of the code, and only there, as a yellow highlight.
export async function getPolicyStatsAPI(reqBody: APIRequestBody) {
const body: APIRequestBody = {
...reqBody,
}
type ExpectedResponse = { data: PolicyStats[] }
const response = await callAPI<ExpectedResponse>({
url: Endpoint.POLICY_STATS,
method: 'POST',
body,
})
return response.data
}
