0

I am using Mocha for testing a Node.js command line app:

 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
        });
    });

I want to use the Mocha framework right, but the only way I can get the nconf var defined in the it() function is to define it outside the before() function. Is the best way to do it?

3
  • 2
    var nconf = null; before(function () { nconf = require('nconf'); nconf.use('memory'); }); Commented May 7, 2015 at 18:01
  • 1
    ^^would look more readable as a code formatted answer :) Commented May 7, 2015 at 18:03
  • 1
    You say you want it defined inside the before call but you do not specify any reason why. If there is no compelling reason for you to call require inside the before then it is wholly a matter of opinion where the call should go. I organize my Mocha files so that they test one library per file. With this organization, there is no discernible benefit to have the require call anywhere else than at the top, with all the other calls. Commented May 7, 2015 at 22:03

1 Answer 1

3

As Yury Tarabanko posted in the comments, the best way to do it is to create the nconf variable outside of the before() and reassign it, on each before run.

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
    });
}); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.