vendredi 27 février 2015

How can I detect if my express (4.0) node.js app is using a specific piece middleware?

For the purposes of Unit testing I'd like to be able to work out if some middleware is in use.


For example


app.js



var express = require('express');
var morgan = require('morgan');

module.exports = function(options) {
var app = express();

if (options.logRequests) {
app.use(morgan('dev'));
}

return app;
}


app.test.js (run via mocha)



describe('app', function(){
it('should log requests when initialized with the `log requests` option', function() {
var app = require('./app)({ logRequests: true });

// Something to establish whether or not morgan is in use
})
})


I believe middleware is pushed to the seemingly undocumented express "stack", which can be accessed via the app._router.stack array, but this looks like a pretty horrible way to get to what I'm after.


Is anyone aware of a better way to this my assertion?


Thanks


Aucun commentaire:

Enregistrer un commentaire