title: Branch coverage zero percent in jest
body: <p>I have written some test cases and everything seems fine except the following one. I am getting zero branch cover for one file. I have googled couple of blog and I came to understand if the statement cab be executed in multiple scenario that call branch coverage. But I don't find my code can be executed in multiple way.</p>
  
  <p>request.js</p>
  
  <pre><code>import axios from 'axios';
  
  export default async (request, httpService = axios) =&gt; {
    const {
      method, url, data, headers,
    } = request;
    return httpService.request({
      method,
      url,
      headers: Object.assign({}, headers),
      data,
    });
  };
  </code></pre>
  
  <p>reqeust.test.js</p>
  
  <pre><code>describe('requestServie', () =&gt; {
    it('should have a valid request object', async () =&gt; {
      const requestObj = {
        method: 'POST',
        url: 'http://mock.url',
        data: {},
      };
  
      const mockRequest = jest.fn(() =&gt; Promise.resolve({}));
  
      const httpService = {
        request: mockRequest,
      };
  
      await request(requestObj, httpService);
      expect(mockRequest).toHaveBeenCalledWith({
        method: requestObj.method,
        url: requestObj.url,
        headers: {},
        data: requestObj.data,
      });
    });
  
    it('should return a valid response (empty)', async () =&gt; {
      const response = {
        data: {
        },
        status: 200,
        statusText: 'OK',
        headers: {},
        config: {},
        request: {},
      };
  
      const mockRequest = jest.fn(() =&gt; Promise.resolve(response));
  
      const httpService = {
        request: mockRequest,
      };
  
      const res = await request({ url: 'http://mock.url' }, httpService);
  
      expect(res).not.toBe(null);
      expect(res).toMatchObject(
        {
          status: response.status,
        },
      );
    });
  });
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>rquest.js</p>
  
  <pre><code>export default async (request, httpService = axios) =&gt; {
    const {
      method, url, data, headers,
    } = request;
    return httpService.request({
      method,
      url,
      headers: Object.assign({}, headers),
      data,
    }).then(successResponse, (error) =&gt; {
      throwHttpError(error);
    });
  };
  </code></pre>
  
  <p>request.test.js</p>
  
  <pre><code>import HttpError from 'standard-http-error';
  import axios from 'axios';
  import request, { successResponse, throwHttpError } from './requestService';
  
  describe('requestService', () =&gt; {
  
    jest.mock('axios', () =&gt; ({
      request: jest.fn(() =&gt; Promise.resolve({})),
    }));
  
    describe('successResponse', () =&gt; {
      const mockRes = {
        status: 9001,
        data: {
          stuff: 'stuff',
        },
      };
      it('should returns an object with only status and data properties', () =&gt; {
        const responseKeys = Object.keys(successResponse(mockRes));
        expect(responseKeys).toMatchObject(['status', 'data']);
        expect(responseKeys.length).toBe(2);
      });
  
      it('should map the status of the reponse to the status property', () =&gt; {
        expect(successResponse(mockRes).status)
          .toBe(mockRes.status);
      });
  
      it('should map the data of the reponse to the data property', () =&gt; {
        expect(successResponse(mockRes).data)
          .toMatchObject(mockRes.data);
      });
  
      it('should have a valid request object', async () =&gt; {
        const requestObj = {
          method: 'POST',
          url: 'http://mock.url',
          data: {},
          headers: {},
        };
  
        const mockRequest = jest.fn(() =&gt; Promise.resolve({}));
  
        const httpService = {
          request: mockRequest,
        };
  
        await request(requestObj, httpService);
        expect(mockRequest).toHaveBeenCalledWith({
          method: requestObj.method,
          url: requestObj.url,
          headers: {},
          data: requestObj.data,
        });
      });
    });
  
    describe('httpThrowError', () =&gt; {
      const mockErr = {
        response: {
          status: 9001,
          statusText: 'error message goes here',
        },
      };
  
      it('should map the status of the reponse to the error.status property', () =&gt; {
        try {
          throwHttpError(mockErr);
        } catch (e) {
          expect(e).not.toBe(null);
          expect(e.status).toBe(mockErr.response.status);
          expect(e.message).toBe(mockErr.response.statusText);
        }
      });
  
      it('should map the data of the reponse to the error.data property', () =&gt; {
        const mockErrWithData = Object.assign({}, mockErr);
        mockErrWithData.response.data = {};
        try {
          throwHttpError(mockErrWithData);
        } catch (e) {
          expect(e).not.toBe(null);
          expect(e.data).toBe(mockErrWithData.response.data);
        }
      });
    });
  
    describe('request', () =&gt; {
      const testCases = [
        ['should return error response on server error', 500],
        ['should return error response on bad request', 400],
        ['should return error response on unauthorised', 401],
      ];
  
      testCases.forEach(([testName, errorStatus]) =&gt; {
        it(testName, async () =&gt; {
          const errorResponse = {
            response: {
              status: errorStatus,
            },
          };
          const mockRequest = jest.fn(() =&gt; Promise.reject(errorResponse));
  
          const httpService = {
            request: mockRequest,
          };
  
          try {
            await request({ url: 'http://mock.url' }, httpService);
            throw new Error('Expected an exception, but none was thrown');
          } catch (err) {
            expect(err).not.toBe(null);
            expect(err).toMatchObject(
              new HttpError(errorResponse.response.status,
                errorResponse.response.statusText),
            );
          }
        });
      });
  
      it('should return an valid response (empty)', async () =&gt; {
        const response = {
          data: {
            meta: {},
            results: [],
          },
          status: 200,
          statusText: 'OK',
          headers: {},
          config: {},
          request: {},
        };
  
        const mockRequest = jest.fn(() =&gt; Promise.resolve(response));
  
        const httpService = {
          request: mockRequest,
        };
  
        const res = await request({ url: 'http://mock.url' }, httpService);
  
        expect(res).not.toBe(null);
        expect(res).toMatchObject(
          {
            status: response.status,
            data: response.data,
          },
        );
      });
  
      it('should use axios by default', async () =&gt; {
        const req = { url: 'http://mock.url', method: 'get' };
        await request(req);
        expect(axios.request).toHaveBeenCalled();
      });
    });
  });
  </code></pre>
  
  <blockquote>
    <p>Error</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/cewQ3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cewQ3.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/opfar.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/opfar.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/CrF79.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CrF79.png" alt="enter image description here"></a></p>
  
  <p><strong>Updated 15/Nov/18</strong></p>
  
  <p><strong>"jest": "^23.6.0",</strong></p>
  
  <pre><code>import HttpError from 'standard-http-error';
  import axios from 'axios';
  import request, { successResponse, throwHttpError } from './requestService';
  
  jest.mock('axios', () =&gt; ({
    request: jest.fn(),
  }));
  </code></pre>
  
  <p><strong>Error</strong></p>
  
  <p><a href="https://i.stack.imgur.com/Oyq0h.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Oyq0h.png" alt="enter image description here"></a></p>