Mocking promised based request with Jest
<p>I'm trying to unit test a function using Jest, and I'm having some trouble dealing with jest mock modules (the equivalent of rewire or proxyquire in nodejs world).</p>

<p>I'm actually trying to test that a spy has been called on the mocked module with some parameters. Here's the function that I want to test.</p>

<p>NB : the current test only concerns the "fetch(...)" part, I m trying to test that fetch has been called with the good parameter.</p>

<pre><code>export const fetchRemote = slug =&gt; {
    return dispatch =&gt; {
        dispatch(loading());
        return fetch(Constants.URL + slug)
            .then(res =&gt; res.json())
            .then(cmp =&gt; {
                if (cmp.length === 1) {
                    return dispatch(setCurrent(cmp[0]));
                }
                return dispatch(computeRemote(cmp));
            });
    };
};
</code></pre>

<p>The function returned acts as a closure, and so "captures" the node-fetch external module that I want to mock.</p>

<p>Here's the test I m trying to make pass green :</p>

<pre><code>it('should have called the fetch function wih the good const parameter and slug', done =&gt; {
            const slug = 'slug';
            const spy = jasmine.createSpy();
            const stubDispatch = () =&gt; Promise.resolve({json: () =&gt; []});
            jest.mock('node-fetch', () =&gt; spy);
            const dispatcher = fetchRemote(slug);
            dispatcher(stubDispatch).then(() =&gt; {
                expect(spy).toHaveBeenCalledWith(Constants.URL + slug);
                done();
            });
        });
</code></pre>

<p>EDIT : The first answer helped a lot concerning writing the test, I have now the following one :</p>

<pre><code>it('should have called the fetch function wih the good const parameter and slug', done =&gt; {
            const slug = 'slug';
            const stubDispatch = () =&gt; null;
            const spy = jest.mock('node-fetch', () =&gt; Promise.resolve({json: () =&gt; []}));
            const dispatcher = fetchRemote(slug);
            dispatcher(stubDispatch).then(() =&gt; {
                expect(spy).toHaveBeenCalledWith(Constants.URL + slug);
                done();
            });
        });
</code></pre>

<p>But now, here's the error I have :</p>

<pre><code> console.error node_modules/core-js/modules/es6.promise.js:117
      Unhandled promise rejection [Error: expect(jest.fn())[.not].toHaveBeenCalledWith()

      jest.fn() value must be a mock function or spy.
      Received:
        object: {"addMatchers": [Function anonymous], "autoMockOff": [Function anonymous], "autoMockOn": [Function anonymous], "clearAllMocks": [Function anonymous], "clearAllTimers": [Function anonymous], "deepUnmock": [Function anonymous], "disableAutomock": [Function anonymous], "doMock": [Function anonymous], "dontMock": [Function anonymous], "enableAutomock": [Function anonymous], "fn": [Function anonymous], "genMockFn": [Function bound getMockFunction], "genMockFromModule": [Function anonymous], "genMockFunction": [Function bound getMockFunction], "isMockFunction": [Function isMockFunction], "mock": [Function anonymous], "resetModuleRegistry": [Function anonymous], "resetModules": [Function anonymous], "runAllImmediates": [Function anonymous], "runAllTicks": [Function anonymous], "runAllTimers": [Function anonymous], "runOnlyPendingTimers": [Function anonymous], "runTimersToTime": [Function anonymous], "setMock": [Function anonymous], "unmock": [Function anonymous], "useFakeTimers": [Function anonymous], "useRealTimers": [Function anonymous]}]
</code></pre>