In the below code npm run test:cov gives coverage less than 100%. In index.html the single quotes are not covered which appears in yellow that the branch not covered.
parsedResponse = (res: string): unknown =>
const Length = Data.length;
const Data = res.split('^^^');
if (Length > 1) {
const firstIndexArray = Data[0].split('^');
let Name: string, topic: string, dob: string;
Name = firstIndexArray[3] || '';//this shows yellow color that is branch not covered (single quote is not covered after or(||))
Topic= secondIndexArray[0] || '';//this shows yellow color that is branch not covered (single quote is not covered after or(||))
const secondIndexArray = Data[1].split('^');
dob = secondIndexArray[1] || '';//this shows yellow color that is branch not covered (single quote is not covered after or(||))
return {
data: this.parsedResponse(result.success.data.string)
};
Test case:
it('should return success', async () => {
jest
.spyOn(httpUtilMockService, 'get')
.mockResolvedValue(EmptyRequestBody);
try {
const res = await service.details(EmptyRequestBody);
expect(res).toMatchObject(SuccessResponseEmpty);
} catch (e) {
expect(
await loggerService.logError('fake-message', { error: e.message })
).toBe(void 0);
}
});
From the test data-
SuccessResponseEmpty:
"data": {
"Name": '',
"Topic": '',
"dob": ''
},
using the above testcase I can't achieve the coverage upto 100%.Please help me to understand that how to test these scenario's?
parsedResponsefunction with an empty string in one of your test case so that when itsplit('^^^')orsplit('^')your indexes will beundefinedthen your|| ''statement will execute. hence it will be covered by your test.