title: Mocking/stubbing objects that are only defined in a closure
body: <p>First of all, for testing my library, I'm using Mocha and Chai, but I'm probably going to need  Sinon too sometime.</p>
  
  <p>This is the library:</p>
  
  <pre><code>import Service from 'service'; // a third-party module out of my control
  
  const service = Service(...);
  
  class MyLib {
      ... uses `service` in a bunch of different ways ...
      ... service.put(foo) ...
      ... service.get(bar) ...
  }
  
  export default MyLib;
  </code></pre>
  
  <p>This is basically the test file:</p>
  
  <pre><code>import MyLib from '../my-lib.js';
  
  describe('MyLib', () =&gt; {
      describe('a method that uses the service', () =&gt; {
          ...
  </code></pre>
  
  <p>The <code>service</code> object makes some calls to remote servers, which I can't really do in the tests. Therefore, I'm thinking I should stub the service's methods or mock the entire service object. However, since the object is constant and only reachable through the <code>MyLib</code> closure, I don't know how.</p>
  
  <p>Ideally I don't wish to change the API of <code>MyLib</code> to e.g. inject the service object in the constructor.</p>
  
  <p>I use Babel 6 with the <code>es2015</code> preset, if it matters.</p>
  
  <p>How should I approach this?</p>