title: Cannot mock a module with jest, and test function calls
body: <p>I create a project using <a href="https://github.com/facebookincubator/create-react-app" rel="noreferrer">create-app-component</a>, which configures a new app with build scripts (babel, webpack, jest).</p>
  
  <p>I wrote a React component that I'm trying to test. The component is requiring another javascript file, exposing a function.</p>
  
  <p>My search.js file</p>
  
  <pre><code>export {
    search,
  }
  
  function search(){
    // does things
    return Promise.resolve('foo')
  }
  </code></pre>
  
  <p>My react component:</p>
  
  <pre><code>import React from 'react'
  import { search } from './search.js'
  import SearchResults from './SearchResults'
  
  export default SearchContainer {
    constructor(){
      this.state = {
        query: "hello world"
      }
    }
  
    componentDidMount(){
      search(this.state.query)
        .then(result =&gt; { this.setState({ result, })})
    }
  
    render() {
      return &lt;SearchResults 
              result={this.state.result}
              /&gt;
    }
  }
  </code></pre>
  
  <p>In my unit tests, I want to check that the method <code>search</code> was called with the correct arguments.</p>
  
  <p>My tests look something like that:</p>
  
  <pre><code>import React from 'react';
  import { shallow } from 'enzyme';
  import should from 'should/as-function';
  
  import SearchResults from './SearchResults';
  
  let mockPromise;
  
  jest.mock('./search.js', () =&gt; {
    return { search: jest.fn(() =&gt; mockPromise)};
  });
  
  import SearchContainer from './SearchContainer';
  
  describe('&lt;SearchContainer /&gt;', () =&gt; {
    it('should call the search module', () =&gt; {
      const result = { foo: 'bar' }
      mockPromise = Promise.resolve(result);
      const wrapper = shallow(&lt;SearchContainer /&gt;);
  
      wrapper.instance().componentDidMount();
  
      mockPromise.then(() =&gt; {
        const searchResults = wrapper.find(SearchResults).first();
        should(searchResults.prop('result')).equal(result);
      })    
    })
  });
  </code></pre>
  
  <p>I already had a hard time to figure out how to make <code>jest.mock</code> work, because it requires variables to be prefixed by <code>mock</code>.</p>
  
  <p>But if I want to test arguments to the method <code>search</code>, I need to make the mocked function available in my tests.</p>
  
  <p>If I transform the mocking part, to use a variable:</p>
  
  <pre><code>const mockSearch = jest.fn(() =&gt; mockPromise)
  jest.mock('./search.js', () =&gt; {
    return { search: mockSearch};
  });
  </code></pre>
  
  <p>I get this error:</p>
  
  <blockquote>
    <p>TypeError: (0 , _search.search) is not a function</p>
  </blockquote>
  
  <p>Whatever I try to have access to the <code>jest.fn</code> and test the arguments, I cannot make it work.</p>
  
  <p>What am I doing wrong?</p>