title: How can I use Jest to spy on a method call?
body: <p>I recently wanted to test that some custom method gets conditionally called in the <code>componentDidMount</code> method of a React component.</p>
  
  <pre><code>componentDidMount() {
    if (this.props.initOpen) {
      this.methodName();
    }
  }
  </code></pre>
  
  <p>I'm using Jest as my testing framework, which includes <code>jest.fn()</code> for mocks/spies.  I've read that this would be fairly trivial to test with Sinon, by doing something like the following:</p>
  
  <pre><code>sinon.spy(Component.prototype, "methodName");
  const wrapper = mount(&lt;Component {...props} /&gt;);
  expect(wrapper.instance().methodName).toHaveBeenCalled();
  </code></pre>
  
  <p>I'm trying to recreate this with Jest like so:</p>
  
  <pre><code>Component.prototype.methodName = jest.fn();
  const wrapper = mount(&lt;Component {...props} /&gt;);
  expect(wrapper.instance().methodName).toHaveBeenCalled();
  </code></pre>
  
  <p>This code fails and throws the following error:</p>
  
  <pre><code>jest.fn() value must be a mock function or spy.
  Received:
    function: [Function bound mockConstructor]
  </code></pre>
  
  <p>Is it possible to test this functionality with Jest? And if so, how?</p>