如何检测我的express(4.0)node.js应用程序是否使用特定的中间件?
为了进行unit testing,我希望能够解决一些中间件是否在使用中。
例如
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 (通过摩卡运行)
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 }) })
我相信中间件被推到看似没有logging的快速“堆栈”,可以通过app._router.stack
数组访问,但是这看起来像是一个非常糟糕的方式来获得我所追求的。
有没有人知道更好的方式来这个我的断言?
谢谢