GarryOne GarryOne - 1 month ago 22
Node.js Question

Stub isAuthenticated written with composable-middleware

I have this middleware function, written with composable-middleware package.

var compose = require('composable-middleware');

module.exports.isAuthenticated = function () {
return compose()
.use(function (req, res, next) {
var authToken = req.get('x-auth-token');
if (!authToken) {
return res.sendStatus(401);
}
next();
});
};


I try stub it with Sinon.js. If it was like this

module.exports.isAuthenticated = function (req, res, next) {
var authToken = req.get('x-auth-token');
if (!authToken) {
return res.sendStatus(401);
}
next();
};


I would have done

sinon.stub(auth, 'isAuthenticated').callsArg(2);


but the problem is that my function uses composable-middleware and I don't know how to stub it.

Answer

Actually, the solution was pretty simple.

var compose = require('composable-middleware');

sinon.stub(auth, 'isAuthenticated', function() {
    return compose()
      .use(function (req, res, next) {
        next();
      });
  });
Comments