title: Mocking an ES6 class function with Jest
body: <p>I have a question about how can I mock an ES6 class instance using Jest which is used by the method I actually want to test.
  My real case is trying to test a Redux async action creator that make a request and dispatch some action depending on the request result.</p>
  
  <p>This is a simplified example of the use case :</p>
  
  <pre><code>// communication.js
  // An exported ES6 class module with proxy to the request library.
  import post from './post';
  export default class communication {
  
      getData(data, success, error) {
          const res = post(data);
          if(res) {
              success();
          } else {
              error();
          }
      }
  }
  
  // communicatorAssist.js
  // A redux async function using communication.js
  import communication from './communication';
  // ...
  
  export function retrieveData() {
      return dispatch =&gt; {
          const data = { name: 'michel'};
          communication.getData(data,
              (res) =&gt; dispatch(successAction(res)),
              (res) =&gt; dispatch(errorAction(res));
      }
  }
  
  // communicatorAssist.test.js testing the communicatorAssist
  import { retrieveData } from 'communicatorAssist';
  
  // communication.getData should call success callback
  // for this test.
  it('Should call the success callback', () =&gt; {
      retrieveData();
      // Assert that mocked redux store contains actions
  });
  
  // communication.getData should call error callback
  // for this test.
  it('Should call the error callback', () =&gt; {
      retrieveData();
      // Assert that mocked redux store contains actions
  });
  </code></pre>
  
  <p>What I want is mocking the communication class in the test and change the behaviour of the <code>getData()</code> function in each test to call <code>success</code> and <code>error</code> callbacks without any call to post method.</p>
  
  <p>I only success to mock the <code>getData()</code> function for the whole test file with this snippet at the top of it :</p>
  
  <pre><code>import communication from '../communication'
  jest.mock('../communication', () =&gt; (() =&gt; ({
      getData: (success, error) =&gt; success()
  })));
  </code></pre>
  
  <p>but I can't switch between implementation in different test case.</p>
  
  <p>I figure that something using <code>.mockImplementation()</code> could do the stuff but I can't make this work in my case (I saw examples using it for module exporting functions but not for classes ).</p>
  
  <p>Does anyone have an idea ?</p>
  
  <p><strong>Edit</strong> :</p>
  
  <p>I forgot a part in the code exemple : the communication class instance creation which I think is the "problem" for mocking it :</p>
  
  <pre><code>const com = new communication();
  </code></pre>
  
  <p>If <code>com</code> is instanciated at a global level in the communicatorAssist.js file : it fail with <em>communication.getData is not a function</em> error.</p>
  
  <p>But if I set the instanciation inside the <code>retrieveData()</code> function, Andreas Köberle snippet work's fine :</p>
  
  <pre><code>import communication from '../communication'
  jest.mock('../communication', () =&gt; jest.fn());
  
  communication.mockImplementation(
    () =&gt; ({
      getData: (success, error) =&gt; success()
    })
  )
  </code></pre>
  
  <p>(<code>jest.mock()</code> factory parameter need to return a function not directly <code>jest.fn</code>)</p>
  
  <p>I don't know why it's not working using a file global scope instance.</p>