1

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?

1
  • 1
    you need to call the parsedResponse function with an empty string in one of your test case so that when it split('^^^') or split('^') your indexes will be undefined then your || '' statement will execute. hence it will be covered by your test. Commented Aug 29, 2023 at 16:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.