title: Node js testing protected routes using mocha chai?
body: <p>The function checks if the route is accessible or not</p>
  
  <pre><code>function isSessionCookieValid(req, res, next) {
        if (!isValid(req.session)) {
          return res.status(401).json({
            isLoggedIn: false
          });
        }
          return next();
   }
  </code></pre>
  
  <p>In another file I do a post request which is a protected route using the above function</p>
  
  <pre><code>  app
       .route('/url')
       .post(utils.isSessionCookieValid, (req, res, next) =&gt; {})
  </code></pre>
  
  <p><strong>TESTING</strong> part</p>
  
  <p>The problem is I don't know how to mock the <strong>isSessionCookieValid</strong> since it requires a next but am not able to pass the next callback in my test:</p>
  
  <pre><code>describe('testing routes', () =&gt; {
    it('should enter the route body', (done) =&gt; {
      utils.isSessionCookieValid(req, res, 'next should be here...');
      chai.request(server)
        .post('/url')
        .end(function (error, response, body) {
          if (error) {
            done(error);
          } else {
            done();
          }
        });
    });
  
  });
  </code></pre>
  
  <p><em>ERROR</em>: TypeError: next is not a function</p>