title: How to properly make mock throw an error in Jest?
body: <p>I'm testing my GraphQL api using Jest.</p>
  
  <p>I'm using a separate test suit for each query/mutation</p>
  
  <p>I have 2 tests (each one in a separate test suit) where I mock one function (namely, Meteor's <code>callMethod</code>) that is used in mutations.</p>
  
  <pre><code>  it('should throw error if email not found', async () =&gt; {
      callMethod
        .mockReturnValue(new Error('User not found [403]'))
        .mockName('callMethod');
  
      const query = FORGOT_PASSWORD_MUTATION;
      const params = { email: 'user@example.com' };
  
      const result = await simulateQuery({ query, params });
  
      console.log(result);
  
      // test logic
      expect(callMethod).toBeCalledWith({}, 'forgotPassword', {
        email: 'user@example.com',
      });
  
      // test resolvers
    });
  </code></pre>
  
  <p>When I <code>console.log(result)</code> I get       </p>
  
  <pre><code>{ data: { forgotPassword: true } }
  </code></pre>
  
  <p>This behaviour is not what I want because in <code>.mockReturnValue</code> I throw an Error and therefore expect <code>result</code> to have an error object</p>
  
  <p>Before this test, however, another is ran</p>
  
  <pre><code> it('should throw an error if wrong credentials were provided', async () =&gt; {
      callMethod
        .mockReturnValue(new Error('cannot login'))
        .mockName('callMethod');
  </code></pre>
  
  <p>And it works fine, the error is thrown</p>
  
  <p>I guess the problem is that mock doesn't get reset after the test finishes.
  In my <code>jest.conf.js</code> I have   <code>clearMocks: true</code></p>
  
  <p>Each test suit is in a separate file, and I mock functions before tests like this:</p>
  
  <pre><code>import simulateQuery from '../../../helpers/simulate-query';
  
  import callMethod from '../../../../imports/api/users/functions/auth/helpers/call-accounts-method';
  
  import LOGIN_WITH_PASSWORD_MUTATION from './mutations/login-with-password';
  
  jest.mock(
    '../../../../imports/api/users/functions/auth/helpers/call-accounts-method'
  );
  
  describe('loginWithPassword mutation', function() {
  ...
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>When I substituted <code>.mockReturnValue</code> with <code>.mockImplementation</code> everything worked out as expected:</p>
  
  <pre><code>callMethod.mockImplementation(() =&gt; {
    throw new Error('User not found');
  });
  </code></pre>
  
  <p>But that doesn't explain why in another test <code>.mockReturnValue</code> works fine...</p>