title: How to mock middleware in Express to skip authentication for unit test?
body: <p>I have the following in Express</p>
  
  <pre><code> //index.js
  
   var service = require('./subscription.service');
   var auth = require('../auth/auth.service');
   var router = express.Router();
  
   router.post('/sync', auth.isAuthenticated, service.synchronise);
  
   module.exports = router;
  </code></pre>
  
  <p>I want to override or mock isAuthenticated to return this</p>
  
  <pre><code>auth.isAuthenticated = function(req, res, next) { 
    return next(); 
  }
  </code></pre>
  
  <p>Here is my unit test:</p>
  
  <pre><code>it('it should return a 200 response', function(done) {
  
    //proxyquire here?
  
    request(app).post('/subscriptions/sync')
    .set('Authorization','Bearer '+ authToken)
    .send({receipt: newSubscriptionReceipt })
    .expect(200,done);
  });
  </code></pre>
  
  <p>I have tried mocking index.js using proxyquire - I think I need to stub the router? 
  I have also tried to override in the test </p>
  
  <pre><code>app.use('/subscriptions', require('./api/subscription'));
  </code></pre>
  
  <p>There must be a simple way to mock this out so I don't need to authenticate the request. Any ideas?</p>