简单的摩卡测试设置

Simple mocha testing setup

我正在使用 Mocha 测试 Node.js 命令行应用程序:

 describe('#call', function () {
        var nconf = require('nconf'); //is this the best place for this?
        before(function () {
            //var nconf = require('nconf'); i'd rather define it here
            nconf.use('memory');
            nconf.set('fp','data_for_testing/csvfile.csv');
            nconf.set('mptp','map_ivr_itg');  
            nconf.set('NODE_ENV','dev_local');
        });
        it('should run without throwing an error or timing out', function (done) {
            var start = require('../lib/setup');
            start.forTesting(done);
            start.run(nconf); //need nconf to be defined here
        });
    });

我想正确使用 Mocha 框架,但要获得 it() 函数中定义的 nconf var 的唯一方法是在 before() 函数之外定义它。是最好的方法吗?

正如 Yury Tarabanko 在评论中发布的那样,最好的方法是在 before() 之外创建 nconf 变量并在每个 运行.

之前重新分配它
describe('#call', function () {
    var nconf = null; // scope of variable is the whole #call tests

    before(function () {
        nconf = require('nconf'); // will reassign before each test
        nconf.use('memory');
        nconf.set('fp','data_for_testing/csvfile.csv');
        nconf.set('mptp','map_ivr_itg');  
        nconf.set('NODE_ENV','dev_local');
    });

    it('should run without throwing an error or timing out', function (done) {
        var start = require('../lib/setup');
        start.forTesting(done);
        start.run(nconf); // nconf is in scope
    });
});