title: How to mock functions in the same module using Jest?
body: <p>What's the best way to correctly mock the following example?</p>
  <p>The problem is that after import time, <code>foo</code> keeps the reference to the original unmocked <code>bar</code>.</p>
  <p><code>module.js</code>:</p>
  <pre><code>export function bar () {
      return 'bar';
  }
  
  export function foo () {
      return `I am foo. bar is ${bar()}`;
  }
  </code></pre>
  <p><code>module.test.js</code>:</p>
  <pre><code>import * as module from '../src/module';
  
  describe('module', () =&gt; {
      let barSpy;
  
      beforeEach(() =&gt; {
          barSpy = jest.spyOn(
              module,
              'bar'
          ).mockImplementation(jest.fn());
      });
  
  
      afterEach(() =&gt; {
          barSpy.mockRestore();
      });
  
      it('foo', () =&gt; {
          console.log(jest.isMockFunction(module.bar)); // outputs true
  
          module.bar.mockReturnValue('fake bar');
  
          console.log(module.bar()); // outputs 'fake bar';
  
          expect(module.foo()).toEqual('I am foo. bar is fake bar');
          /**
           * does not work! we get the following:
           *
           *  Expected value to equal:
           *    &quot;I am foo. bar is fake bar&quot;
           *  Received:
           *    &quot;I am foo. bar is bar&quot;
           */
      });
  });
  </code></pre>
  <p>I could change:</p>
  <pre><code>export function foo () {
      return `I am foo. bar is ${bar()}`;
  }
  </code></pre>
  <p>to:</p>
  <pre><code>export function foo () {
      return `I am foo. bar is ${exports.bar()}`;
  }
  </code></pre>
  <p>but this is pretty ugly in my opinion to do everywhere.</p>