title: Mocking methods in Jest
body: <p>I have a very simple unit test that looks like this:</p>
  
  <pre><code>import ApiWrapper from '../../services/api_wrapper';
  jest.unmock('../helper')
  
  describe('Helper', () =&gt; {
      let Helper;
  
      beforeEach(() =&gt; {
          Helper = require('../helper').default;
      });
  
      it('calls the Api Wrapper', () =&gt; {
          Helper.help()
  
          expect(ApiWrapper.help).toHaveBeenCalled();
      });
  
  });
  </code></pre>
  
  <p>Where Helper looks like this:</p>
  
  <pre><code>import ApiWrapper from '../services/api_wrapper'
  class Helper {
      help() {
          ApiWrapper.help()
      }
  }
  
  export default new Helper();
  </code></pre>
  
  <p>And ApiWrapper looks like this:</p>
  
  <pre><code>class ApiWrapper {
    static help () {
       console.log('help!')
    }
  }
  export default ApiWrapper;
  </code></pre>
  
  <p>ApiWrapper.help() gets mocked by Jest so 'help!' never gets printed, yet the expectation in the test fails. This still fails if we rewrite ApiWrapper to just be a plain Javascript object like such:</p>
  
  <pre><code>export default {
      help: () =&gt; { console.log('help!'); }
  }
  </code></pre>
  
  <p>It works, however, if we change the imports in the spec (so ApiWrapper is imported in the beforeEach), and rewrite ApiWrapper to be a Singleton class, like so:</p>
  
  <pre><code>class ApiWrapper {
     help() {
        console.log('help!');
     }
  }();
  
  export default new ApiWrapper();
  </code></pre>
  
  <p>What is it about Jest's mocking behavior that makes this happen?</p>