title: Jest how to mock api call
body: <p>I am trying to mock my api call with jest but for some reason it's not working. I don't really understand why. Anyone has an idea?</p>
  
  <p>(the test keep call the original api call function and not the mock)</p>
  
  <p>my test.js</p>
  
  
  
  <pre class="lang-js prettyprint-override"><code>import { getStuff } from '../stuff';
  import * as api from '../../util/api';
  
  describe('Action getStuff', () =&gt; {
          it('Should call the API to get stuff.', () =&gt; {
              api.call = jest.fn();
              getStuff('slug')(() =&gt; {}, () =&gt; {});
              expect(api.call).toBeCalled();
              jest.unmock('../../util/api.js');
          });
  });
  </code></pre>
  
  <p>stuff.js redux action</p>
  
  <pre class="lang-js prettyprint-override"><code>import api from '@util/api';
  import { STUFF, API } from '../constant';
  
  
  export const getStuff = slug =&gt; (dispatch, getState) =&gt; {
      const state = getState();
      api.call(API.STUFF.GET, (err, body) =&gt; {
          if (err) {
              console.error(err.message);
          } else {
              dispatch({
                  type: STUFF.GET,
                  results: body,
              });
          }
      }, {
          params: { slug },
          state
      });
  };
  </code></pre>