-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-hooks.js
92 lines (80 loc) · 2.44 KB
/
test-hooks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var testName = 'hooks';
var test = require('tape');
var path = require('path');
var merge = require('deeply');
var configly = require('../configure');
var expected = require('./fixtures/expected/' + testName + '.json');
var configDir = path.join(__dirname, 'fixtures/config/' + testName);
test('custom hooks', function(t)
{
t.plan(1);
var configObj
, customHooks = merge(configly.hooks,
{
// it will be ignored, since won't match any file
nomatch: function()
{
throw new Error('What am I doing here?');
},
zomatch: function()
{
throw new Error('Should no be called either');
},
// will be applied to `default` file only
// expects `config` object of the parsed file
default: function(config)
{
iterate(config, function(value, key, node)
{
// increments each value by 3
node[key] = value + 3;
});
// expected to return updated object
return config;
},
// will be applied to both `local` and `local-development` files
// expects `config` object of the parsed file
local: function(config)
{
iterate(config, function(value, key, node)
{
// multiplies each value by 2
node[key] = value * 2;
});
// expected to return updated object
return config;
},
// will be applied to `local-development` file only
// expects `config` object of the parsed file
'local-development': function(config)
{
iterate(config, function(value, key, node)
{
// increments each value by 5
node[key] = value + 5;
});
// expected to return updated object
return config;
}
})
;
process.env['NODE_ENV'] = 'development';
configObj = configly({directories: configDir, hooks: customHooks});
t.deepLooseEqual(configObj, expected, 'expects to get proper config object');
});
/**
* Iterates two levels deep
*
* @param {object} config - config object to iterate over
* @param {function} iterator - invoked with each config value
*/
function iterate(config, iterator)
{
Object.keys(config).forEach(function(key)
{
Object.keys(config[key]).forEach(function(subkey)
{
iterator(config[key][subkey], subkey, config[key]);
});
});
}