title: Jest: How to mock a promise on the same file for resolve and reject options?
body: <p>So I am attempting to test the positive and negative sides of my implementation. If I do this:</p>
  
  <pre><code>jest.mock('./apiClient', () =&gt; ({
    get: jest.fn((url: string) =&gt; Promise.resolve({ data: mockData }))
  }));
  
  jest.mock('./apiClient', () =&gt; ({
    get: jest.fn().mockImplementation((url: string) =&gt; {
      console.log('error result');
      return Promise.reject(mockError);
    })
  }));
  </code></pre>
  
  <p>Then the only test passing will be the last one declared. In the above case, it will be the <code>reject</code> case which will pass and the first mock will be ignored.</p>
  
  <p>This is my whole test:</p>
  
  <pre><code>// @ts-ignore
  import ApiClient from './apiClient';
  import ApiService from './apiService';
  
  const mockData = {};
  const mockError = { message: 'Smth Bad Happened' };
  
  const firstCallback = jest.fn((data: any) =&gt; data);
  const secondCallback = jest.fn((data: any) =&gt; data);
  
  jest.mock('./apiClient', () =&gt; ({
    get: jest.fn((url: string) =&gt; Promise.resolve({ data: mockData }))
  }));
  
  jest.mock('./apiClient', () =&gt; ({
    get: jest.fn().mockImplementation((url: string) =&gt; {
      console.log('error result');
      return Promise.reject(mockError);
    })
  }));
  
  describe('apiService', () =&gt; {
    it('should call callbacks consequently', done =&gt; {
      ApiService.makeApiCall('testUrl', firstCallback, secondCallback).then(() =&gt; {
        expect(firstCallback).toBeCalledTimes(1);
        expect(firstCallback).toBeCalledWith(mockData);
  
        expect(secondCallback).toBeCalledTimes(1);
        expect(secondCallback).toBeCalledWith(firstCallback(mockData));
        done();
      });
    });
  
    it('should handle error', done =&gt; {
      console.error = jest.fn();
      ApiService.makeApiCall('testUrl', firstCallback, secondCallback).then(() =&gt; {
        expect(firstCallback).toBeCalledTimes(0);
        expect(secondCallback).toBeCalledTimes(0);
        expect(console.error).toBeCalledTimes(1);
        expect(console.error).toBeCalledWith('ApiClient testUrl', mockError);
        done();
      });
    });
  });
  </code></pre>
  
  <p>As it is right now, the test passing is <code>should handle error</code> which is the second one, but if I have switch the positions from </p>
  
  <pre><code>jest.mock('./apiClient', () =&gt; ({
    get: jest.fn((url: string) =&gt; Promise.resolve({ data: mockData }))
  }));
  
  jest.mock('./apiClient', () =&gt; ({
    get: jest.fn().mockImplementation((url: string) =&gt; {
      console.log('error result');
      return Promise.reject(mockError);
    })
  }));
  </code></pre>
  
  <p>To</p>
  
  <pre><code>jest.mock('./apiClient', () =&gt; ({
    get: jest.fn().mockImplementation((url: string) =&gt; {
      console.log('error result');
      return Promise.reject(mockError);
    })
  }));
  
  jest.mock('./apiClient', () =&gt; ({
    get: jest.fn((url: string) =&gt; Promise.resolve({ data: mockData }))
  }));
  </code></pre>
  
  <p>then the test passing will be <code>should call callbacks consequently</code>, so what can I do to mock both reject and resolve without interfering one with the other?</p>