How to mock middleware in Express to skip authentication for unit test?

You can use sinon to stub isAuthenticated method, but you should do that before a reference to auth.isAuthenticated is set as a middleware, so before you require the index.js and app is created. Most likely you would want this in a beforeEach hook: var app; var auth; beforeEach(function() { auth = require(‘../wherever/auth/auth.service’); sinon.stub(auth, ‘isAuthenticated’) .callsFake(function(req, …

Read more