title: Internal vs Exported References to Module Methods
body: <p>I'm trying to better understand the how &amp; why of differences between references to an internal module method vs the reference that is created when that same method is exported. </p>
  
  <p>I'm not even sure my terminology is correct, and searching for information has not turned up anything useful.</p>
  
  <p>A simplified example ...I have a simple module that performs some maths:</p>
  
  <pre><code>export const doubleNumber = (num) =&gt; {
    return num * 2
  }
  
  export const squareNumber = (num) =&gt; {
    return num * num
  }
  
  export default {
    double: (num) =&gt; doubleNumber(num),
    square: (num) =&gt; squareNumber(num),
  }
  
  </code></pre>
  
  <p>...and I have some unit tests to verify functionality:</p>
  
  <pre><code>import * as numProvider from './number-provider'
  
  describe('the default methods', () =&gt; {
    it('should call the correct math method when its property method is called', () =&gt; {
      const doubleSpy = jest.spyOn(numProvider, 'doubleNumber')
  
      const result = numProvider.default.double(1)
  
      expect(result).toBe(2)
      expect(doubleSpy).toHaveBeenCalledTimes(1)
    })
  })
  </code></pre>
  
  <p>(<a href="https://github.com/BenjaminWFox/javascript-module-tests-reference" rel="nofollow noreferrer">example project with the above code</a>)</p>
  
  <p>Now when I run my test it fails. From what I understand, the method I'm spying on (the exported method), is never called because <code>numProvider.default.double</code> is referencing the internal method.</p>
  
  <p>I can verify this (and fix the test) by attaching the exported method to the default export object instead, <code>double: (num) =&gt; exports.doubleNumber(num)</code> but then of course the site breaks because <code>exports</code> is not defined in the browser.</p>
  
  <p><strong>So my question (I think?) is..</strong></p>
  
  <p>What is JavaScript (or some other process?) doing that causes the creation of these two separate references?</p>