我是NodeJS / Express的新手,我遇到了与模块和配置设置的全局/模块共享有关的问题,我将在这里解释一下。
一个简单的例子是尝试对包含其他库的中间件模块进行单元测试。如下:
配置/ settings.js
module.exports = {
...,
debugLevel: 3
};
中间件/ auth.js
// Required modules for middleware
// ----------------------------------------------------------------------
var config = require('../config/settings');
var log = require('../libraries/logger')('middlewares/auth');
...
// Middleware methods
// ----------------------------------------------------------------------
var middleware = {};
middleware.apicall = function(req, res, next) {
...
log.info('Made api call');
};
库/ logger.js
// External Dependencies
var winston = require('winston');
var winlogger = new (winston.Logger)({
transports: [
new (winston.transports.Console)()
]
});
winlogger.cli();
var logger = function(module, debug) {
this.module = module;
this.logger = winlogger;
this.debugLevel = debugLevel;
};
logger.prototype = {
module: '',
logger: null,
debugLevel: 3,
/**
* Generic Log function
*
* @param level
* @param str
* @param data
*/
debug: function(level, str, data) {
if ( this.debugLevel > 2) this.logger.log(level, this.module+' - '+str, data);
},
/**
* Specific function for info
*
* @param str
* @param data
*/
info: function(str, data) {
if ( this.debugLevel > 1 ) this.logger.info(this.module+' - '+str, data);
}
};
测试/ api.js
const chai = require('chai');
const should = chai.should;
const expect = chai.expect;
const assert = chai.assert;
// Node Http Mocks
var httpMocks = require('node-mocks-http');
var middleware = require('../middlewares/auth');
describe("Test API Call", function() {
it("should give 200 over api call", function(done) {
var req = httpMocks.createRequest({
method: 'GET',
url: '/test',
params: {}
});
var res = httpMocks.createResponse();
middleware.apicall(req, res, function(res) { return res; });
assert.equal(res.statusCode, 200);
done();
});
});
现在这一切都有效,但我遇到的问题是:如何在没有显示apicall方法的日志行的情况下进行测试?
设置是否可以全局存在?或者一种构建应用程序的方法,以便您可以在app.js中注入来自单个需求的设置?
我需要在tests / api.js中做这样的事情:
var config = require('../config/settings');
config.debug = 0;
并将其注入测试中。任何人都面临类似的挑战?
答案 0 :(得分:0)
<强> ANSWER 强>
在没有找到解决方案的情况下,我尝试进行一系列测试,并提出了一个有效的解决方案。
我们在这种特殊情况下使用全局变量。所以:
<强>仓\ WWW 强>
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
...
var http = require('http');
global.config = require('../config/settings');
global.log = require('../libraries/logger')('server');
...
<强> app.js 强>
// Node framework dependencies
// ----------------------------------------------------------------------
var express = require('express');
var config = global.config;
var path = require('path');
var log = global.log;
log.setLabel('app');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var cors = require('cors');
var validator = require('express-validator');
...
<强>测试\ api.js 强>
const chai = require('chai');
const should = chai.should;
const expect = chai.expect;
const assert = chai.assert;
global.config = require('../config/settings');
config.debug = false;
global.log = require('../libraries/logger')('Test case');
...
// Node Http Mocks
var httpMocks = require('node-mocks-http');
var middleware = require('../middlewares/auth');
describe("Test API Call", function() {
it("should give 200 over api call", function(done) {
var req = httpMocks.createRequest({
method: 'GET',
url: '/test',
params: {}
});
var res = httpMocks.createResponse();
middleware.apicall(req, res, function(res) { return res; });
assert.equal(res.statusCode, 200);
done();
});
});