nconf icon indicating copy to clipboard operation
nconf copied to clipboard

reset doesn't reset?

Open jchip opened this issue 11 years ago • 5 comments

What is reset supposed to do?

This piece of code: var nconf = require('nconf');

nconf.use('test1', { type: 'literal', store: { 'item1': 'item1' } }); nconf.use('test2',{ type: 'literal', store: { 'item2': 'item2' } }); console.log(nconf.get('item1')); console.log(nconf.get('item2')); nconf.reset(); console.log('------ reset -----'); console.log(nconf.get('item1')); console.log(nconf.get('item2'));

produced this output: item1 item2 ------ reset ----- item1 item2

jchip avatar Dec 03 '13 07:12 jchip

Also scratching my head over this one. I can't find ANY way to get nconf to reset, which means I can't run mocha with tests with different overrides. It's a mystery to me why overrides can't be called at any time.

lastobelus avatar Sep 19 '14 05:09 lastobelus

This was pretty frustrating to figure out (altering a loaded nconf for tests) so for posterity here is what I did in my config.js file

'use strict';

var nconf = require('nconf');

module.exports = function (overrides) {
    if(overrides == undefined){
        overrides = {};
    }
    nconf.overrides(overrides).argv().env().
        file('user', nconf.get('user') || './user.json').
        file('global','./global.json').
        defaults(require('./config.json'));
    return nconf;
}

then in tests instead of require('./config.js') I can do require('./config.js')({overridden: "for this test"})

lastobelus avatar Sep 19 '14 06:09 lastobelus

@jchip As far as I understand, nconf.reset() is an asynchronous method. I suppose your code should be like this:

var nconf = require('nconf');

nconf.use('test1', { type: 'literal', store: { 'item1': 'item1' } });
nconf.use('test2',{ type: 'literal', store: { 'item2': 'item2' } });
console.log(nconf.get('item1'));
console.log(nconf.get('item2'));
nconf.reset(err=>{
  if (err) {
    console.error(err);
    return;
  }
  console.log('------ reset -----');
  console.log(nconf.get('item1'));
  console.log(nconf.get('item2'));
});

Sorry for reanimating so old issue - it is still not closed.

yarick123 avatar Oct 12 '18 15:10 yarick123

this doesn't work for me with a callback as well.

srolel avatar Oct 12 '20 22:10 srolel

I can't get any permutation of this working whatsoever. My particular application runs nconf.env().use('memory') to source configuration from environment variables and I'm trying to unit test several configuration scenarios but I'm having trouble clearing out nconf before each unit test. Does anyone have any guidance about reseting configuration across the board?

willvedd avatar Mar 10 '22 20:03 willvedd