title: How Do I Stub webpack's require.ensure?
body: <p>I use <a href="https://webpack.js.org" rel="noreferrer">webpack</a>'s code splitting feature (<a href="https://webpack.js.org/guides/code-splitting-async/#require-ensure-" rel="noreferrer">require.ensure</a>) to reduce the initial bundle size of my <a href="https://facebook.github.io/react/" rel="noreferrer">React</a> application by loading components that are not visible on page load from a separate bundle that is loaded asynchronously.</p>
  
  <p>This works perfectly, <em>but</em> I have trouble writing a unit test for it.</p>
  
  <p>My test setup is based on <a href="https://mochajs.org" rel="noreferrer">Mocha</a>, <a href="http://chaijs.com" rel="noreferrer">Chai</a> and <a href="http://sinonjs.org" rel="noreferrer">Sinon</a>.</p>
  
  <p>Here is the relevant excerpt from the code I have tried so far:</p>
  
  <pre><code>describe('When I render the component', () =&gt; {
      let component,
          mySandbox;
      beforeEach(() =&gt; {
          mySandbox = sandbox.create();
          mySandbox.stub(require, 'ensure');
          component = mount(&lt;PageHeader /&gt;);
      });
      describe('the rendered component', () =&gt;
          it('contains the SideNav component', () =&gt;
              component.find(SideNav).should.have.length(1)
          )
      );
      afterEach(() =&gt; mySandbox.restore());
  });
  </code></pre>
  
  <p>When running the test, I get this error message:</p>
  
  <blockquote>
    <p>"before each" hook for "contains the SideNav component": Cannot stub non-existent own property ensure</p>
  </blockquote>
  
  <p>This happens because <code>require.ensure</code> is a method that only exists in a webpack bundle, but I'm not bundling my tests with webpack, nor do I want to, because it would create more overhead and presumably longer test execution times.</p>
  
  <p>So my question is:</p>
  
  <p><strong>Is there a way to stub webpack's <code>require.ensure</code> with Sinon without running the tests through webpack?</strong></p>