title: Jest: mocking conditional function calls
body: <p>I am trying to write a test to make sure that, when appropriate, a particular function (in this case, a sentry function) is called with a particular message. However, when I write this test, it fails, and I get the following message. How to I properly mock the <code>captureMessage</code> function in <code>handleError.test.js</code> to make sure that it is properly called with the <code>"this is an error message."</code> string in <code>handleError.js</code>? Thanks!</p>
  
  <p><strong>error message:</strong></p>
  
  <blockquote>
    <p>Error: expect(jest.fn())[.not].toHaveBeenCalledWith()</p>
    
    <p>jest.fn() value must be a mock function or spy. Received:   function:
    [Function captureMessage]</p>
  </blockquote>
  
  <p><strong>handleError.js:</strong></p>
  
  <pre><code>import {captureMessage} from '@sentry/browser';
  
  const handleError = (error) =&gt; {
    if (error.name === "ApiError") {
      captureMessage('this is an error message.');
    }
  };
  
  export default handleError;
  </code></pre>
  
  <p><strong>handleError.test.js:</strong></p>
  
  <pre><code>import {captureMessage} from '@sentry/browser';
  import handleError from '../handleError';
  
  class ApiError extends Error {
    constructor() {
      super();
      this.name = 'ApiError';
    }
  }
  
  test('When an ApiError is returned with no action type, sentry is notified', () =&gt; {
    const sampleError = new ApiError();
    handleError(sampleError);
    expect(captureMessage).toHaveBeenCalledWith('this is an error message.');
  });
  </code></pre>