title: Jest spying on function called from within another
body: <p>I have the following two files:</p>
  
  <p>functions.js</p>
  
  <pre><code>function getOne() {
    return 1;
  }
  
  function getTen() {
    let val = 0;
    for (let x = 0; x &lt; 10; x++) {
      val+= getOne();
    }
    return val;
  }
  
  module.exports = {
                    getOne,
                    getTen,
                   }
  </code></pre>
  
  <p>functions.test.js</p>
  
  <pre><code>const numberFunctions = require('../functions.js');
  const getOne = numberFunctions.getOne;
  const getTen = numberFunctions.getTen;
  
  // passes
  test('I should be able to get the number 1', () =&gt; {
    expect(getOne()).toBe(1);
  });
  
  describe('The getTen function', () =&gt; {
    // passes
    it('should return 10', () =&gt; {
      expect(getTen()).toBe(10);
    });
  
    // fails
    it('should call the getOne method 10 times', () =&gt; {
      const spy = jest.spyOn(numberFunctions, 'getOne');
      expect(spy).toHaveBeenCalledTimes(10);
    });
  });
  </code></pre>
  
  <p>I'm trying to ensure that the the function getOne has been called from within the getTen function. In total it should be called 10 times, but my spy always claims that it has been called <strong>zero</strong> times.</p>
  
  <p>I've tried rearranging my test so that I mock the getOne function as a global e.g.</p>
  
  <pre><code>it('should call the getOne method 10 times', () =&gt; {
      global.getOne = jest.fn(() =&gt; 1);
      expect(global.getOne).toHaveBeenCalledTimes(10);
    });
  </code></pre>
  
  <p>But this results in the same outcome. How can I spy on the getOne function being called from within the getTen function?</p>