I am encountering an issue throughout my application:
When running Jest test coverage script with Istanbul, I am getting a "branch not covered" message specifically on the async function
part of my well covered function. What does this message mean and how can I address it?
I am not sure what exactly is uncovered in these functions.
For example, here is a test case for the "getPolicyStatsAPI" function: The remaining functions are tested in a similar manner. I don't think the solution lies within the test suites, but I can provide them if necessary.
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)
})
})
The section of code triggering the "branch not covered" warning is as follows: The warning highlight specifically appears over the "async function" part of the code, marked in yellow.
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
}