title: Jestjs how to test function being called inside another function
body: <p>For testing I use jest and react-test-renderer. It should be simple to test, however I have a hard time finding the proper example. I have tried to do something like that (in general I keep the functions in separate files):</p>
  
  <p>utils.js</p>
  
  <pre><code>export const childFunction = () =&gt; 'something';    
  const parentFunction = () =&gt; childFunction();
  export default parentFunction;
  </code></pre>
  
  <p>utils.test.js</p>
  
  <pre><code>import parentFunction from './utils.js';
  
  
  it('childFunction should be called', () =&gt; {
   const childFunction = jest.fn();
   parentFunction();
   expect(childFunction).toBeCalled();
  })
  </code></pre>
  
  <p>The fragment <em>const childFunction = jest.fn();</em> definitely won't work. While calling, parentFunction's body cares only about its own scope. But it also won't work if I import childFunction and do <em>jest.mock(childFunction)</em>, because jest.mock needs a string, a url to a module, and not the function itself.</p>
  
  <p>The example above doesn't work and I'm searching for an alternative. However this works after rendering the component with ShallowRenderer. And I'd like to achieve a similar behaviour with a function nested inside another function.</p>
  
  <pre><code>class Component extends React.Component {
   componentDidMount() {parentFunction()}
   render() {...}
  }
  
  const renderer = new ShallowRenderer();
  describe("testing parentFunction", () =&gt; {
    renderer.render(&lt;Component/&gt;);
    it("parentFunction should be called", () =&gt; {
      expect(parentFunction).toBeCalled();
    });
  });
  </code></pre>