title: How to simulate time delay (timeout) when mocking method calls in unit testing with proxyquire and mocha?
body: <p>Using  proxyquire I'm mocking a method of module B (injected with require() in module A), when testing the method in the module A. The mock (mocking get_campaigns method of admitad.model.js module):</p>
  <pre class="lang-js prettyprint-override"><code>const admitadModelMock = {
      '../services/admitad.model': {
          get_campaigns: (limit, page) =&gt; new Promise((resolve, reject) =&gt;
              setTimeout(resolve({campaigns: testData, count: 1000}), 5000)
          ),
      },
  };
  </code></pre>
  <p>The test:</p>
  <pre class="lang-js prettyprint-override"><code>it('shold get all campaigns from Admitad', async function () {
      this.timeout(60000);
      let err, data;
      // mock dependencie (get_campaigns() of module B will be mocked):
      let $serviceStubbed = proxyquire('../services/campaign-sync', admitadModelMock);
      // getAdmitadCampaigns() just calls get_campaigns method of module B
      [err, data] = await to($serviceStubbed.getAdmitadCampaigns(50));
      data.length.should.be.equal(50);
  });
  </code></pre>
  <p>The problem is the test passing without expected delay of 5 seconds.</p>
  <p><strong>Update.</strong></p>
  <p>This is working:</p>
  <pre><code>setTimeout(() =&gt; resolve({campaigns: mockedCampaigns, count: 1000}), 2000)
  </code></pre>
  <p>Here is my final nice approach:</p>
  <pre class="lang-js prettyprint-override"><code>// helper to wrap timeout generation
  const timer = (data, time) =&gt;
      new Promise((resolve, reject) =&gt;
          setTimeout(() =&gt; resolve(data), time)
      );
  
  // Factory function to generate mock with data we need
  const blacklistedModelMockFactory =
      (onRead = [], onUpdate = 'Ok', onCreate = 'Ok', onDelete = 'Ok') =&gt; ({
          '../services/campaigns-blacklist.model': {
              read: () =&gt; timer(onRead, 2000),
              update: () =&gt; timer(onUpdate, 2000),
              create: () =&gt; timer(onCreate, 1000),
              delete: () =&gt; timer(onDelete, 1000),
          },
      });
  
  // Test example
  it('should filter Registered and Blacklisted collections', async function () {
      this.timeout(60000);
      $service.should.have.property('filterRB').a('function');
      const sourceRegistered = mockedCampaigns.slice(5, 10);
      const sourceBlacklisted = mockedCampaigns.slice(15, 18);
      let error, success;
      // mock dependencies in tested module with our expected data:
      let $serviceStubbed = proxyquire(
          '../services/campaign-sync', Object.assign(
              {},
              blacklistedModelMockFactory(sourceBlacklisted),
              registeredModelMockFactory(sourceRegistered)
          )
      );
      [error, success] = await to($serviceStubbed.filterRB(mockedCampaigns));
      expect(error).to.be.equal(null);
      success.filtered.length.should.be.equal(12);
      success.blacklisted.length.should.be.equal(3);
      success.registered.length.should.be.equal(5);
  });
  </code></pre>