Open jchip opened 10 years ago
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.
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"})
@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.
this doesn't work for me with a callback as well.
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?
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