UNPKG

8.87 kBJavaScriptView Raw
1const assert = require('assert'),
2 common = require('./common'),
3 path = require('path');
4
5const { _getDeploymentConfig,
6 _getIndexConfig,
7 clone,
8 get,
9 getAllConfigs,
10 getConfig,
11 merge,
12 mergeWith } = require('../src/lib');
13
14const { compressEnv,
15 _defaultMerge,
16 _getMerger,
17 _isEnvProp,
18 _isValueType,
19 _parseKey } = require('../src/lib/object');
20
21const configPath = path.join(__dirname, './config');
22const options = { path: configPath };
23
24describe('lib module', function() {
25
26 describe('"dir"', function() {
27 const dir = require('../src/lib/dir');
28
29 it('rootPath()', function() {
30 assert.strictEqual(dir.appRoot(), path.join(__dirname, '..'));
31 });
32 });
33
34 describe('"env"', function() {
35 const env = require('../src/lib/env');
36
37 it('_isNpmEnvSet()', function() {
38 process.env.npm_ = "set for testing";
39 assert.strictEqual(env._isNpmEnvSet(), true);
40 delete process.env.npm_;
41 assert.ok(!process.env.npm_);
42 });
43
44 it('_getPackageConfigPath()', function() {
45 let dir = path.join(__dirname, '../');
46 let packageConfigPath = env._getPackageConfigPath(dir);
47 assert.ok(packageConfigPath);
48 assert.strictEqual(
49 packageConfigPath,
50 './test/config');
51 });
52
53 it('_getEnvConfigPath()', function() {
54 const APP_CONFIG_PATH = "CONFIG_PATH";
55 process.env[APP_CONFIG_PATH] = './test/config';
56 let envConfigPath = env._getEnvConfigPath();
57 assert.ok(envConfigPath);
58 assert.strictEqual(
59 envConfigPath,
60 './test/config');
61 delete process.env[APP_CONFIG_PATH];
62 assert.ok(!process.env[APP_CONFIG_PATH]);
63
64 // npm_packag_config_configPath
65 // process.env.npm_package_config_config_path = './test/config';
66 // envConfigPath = env._getEnvConfigPath();
67 // assert.ok(envConfigPath);
68 // assert.strictEqual(
69 // envConfigPath,
70 // './test/config');
71 // delete process.env.npm_package_config_config_path;
72 // assert.ok(!process.env.npm_package_config_config_path);
73
74 });
75
76 it('getConfigPath()', function() {
77 assert.strictEqual(
78 env.getConfigPath(),
79 configPath);
80
81 // package.json has a config value for config path but the module isn't defaulting to ./config. How would I test for that since it will pick up the value if there is one in package.json.
82 });
83 });
84
85 describe('file', function() {
86 //const file = require('../src/lib/file');
87
88 describe('_getIndexConfig', function() {
89 let indexConfig = _getIndexConfig(options);
90
91 it('gets index config object', function() {
92 assert.ok(indexConfig);
93 });
94
95 it('all values are as expected', function() {
96 assert.strictEqual(indexConfig.logging, false);
97 //console.log(indexConfig.get("db"));
98 assert.strictEqual(
99 get(indexConfig, "db.username"), null);
100 assert.strictEqual(indexConfig.db.password, null);
101 assert.strictEqual(indexConfig.db.host, null);
102 assert.strictEqual(indexConfig.db.port, 27017);
103 assert.strictEqual(indexConfig.db.database, null);
104 });
105 });
106
107
108 describe('_getDeploymentConfig', function() {
109 let saved = {};
110
111 before(function() {
112 saved.NODE_ENV = process.env.NODE_ENV;
113 saved.TEST = process.env.TEST;
114 });
115
116 describe('development values', function() {
117 let dev;
118
119 before(function() {
120 process.env.NODE_ENV = "development";
121 delete process.env.TEST;
122
123 dev = _getDeploymentConfig("development", options);
124 });
125
126 it('NODE_ENV = "development"', function() {
127 assert.strictEqual(process.env.NODE_ENV, "development");
128 });
129
130 it('TEST = undefined', function() {
131 assert.strictEqual(process.env.hasOwnProperty("TEST"), false);
132 assert.strictEqual(process.env.TEST, undefined);
133 });
134
135 it('logging ok', function() {
136 assert.strictEqual(dev.logging, true);
137 });
138
139 it('username ok', function() {
140 assert.strictEqual(dev.db.username, "app");
141 });
142
143 it('password ok', function() {
144 assert.strictEqual(dev.db.password, "password123");
145 });
146
147 it('host ok', function() {
148 assert.strictEqual(dev.db.host, "localhost");
149 });
150
151 it('port ok', function() {
152 assert.strictEqual(dev.db.port, 27017);
153 });
154
155 it('database ok', function() {
156 assert.strictEqual(dev.db.database, "dev");
157 });
158 });
159
160 describe('testing values', function() {
161 let test;
162
163 before(function() {
164 process.env.NODE_ENV = "development";
165 process.env.TEST = "TEST";
166 test = _getDeploymentConfig("development", options);
167 });
168
169 it('NODE_ENV = "development"', function() {
170 assert.strictEqual(process.env.NODE_ENV, "development");
171 });
172
173 it('TEST = "TEST"', function() {
174 assert.strictEqual(process.env.TEST, "TEST");
175 });
176
177 it('logging ok', function() {
178 assert.strictEqual(test.logging, false);
179 });
180
181 it('db username ok', function() {
182 assert.strictEqual(test.db.username, "app");
183 });
184
185 it('db password ok', function() {
186 assert.strictEqual(test.db.password, "password123");
187 });
188
189 it('db host ok', function() {
190 assert.strictEqual(test.db.host, "127.0.0.1");
191 });
192
193 it('db port ok', function() {
194 assert.strictEqual(test.db.port, 27017);
195 });
196
197 it('db database ok', function() {
198 assert.strictEqual(test.db.database, "test");
199 });
200 });
201
202 after(function() {
203 process.env.NODE_ENV = saved.NODE_ENV;
204 process.env.TEST = saved.TEST;
205 });
206
207 }); // getDeployment
208
209
210 describe('getConfig', function() {
211 let saved = {};
212
213 before(function() {
214 saved.NODE_ENV = process.env.NODE_ENV;
215 saved.TEST = process.env.TEST;
216 });
217
218 beforeEach(function() {
219 delete process.env.NODE_ENV;
220 delete process.env.TEST;
221 });
222
223 it('default config validates', function() {
224 let config = getConfig();
225
226 assert.strictEqual(config('#env'), 'development');
227 common.assert_development_config(config);
228 });
229
230 it('development config validates', function() {
231 let config = getConfig('development');
232
233 common.assert_development_config(config);
234 });
235
236 it('development+TEST config validates', function() {
237 process.env.TEST = "TEST";
238 let config = getConfig('development');
239
240 common.assert_development_plus_TEST_config(config);
241 });
242
243 it('staging config validates', function() {
244 process.env.NODE_ENV = 'staging';
245 let config = getConfig('staging', options);
246
247 common.assert_staging_config(config);
248 });
249
250 after(function() {
251 process.env.NODE_ENV = saved.NODE_ENV;
252 process.env.TEST = saved.TEST;
253 });
254 });
255
256
257 describe('getAllConfigs', function() {
258 // TODO:!!!
259 // ...w/out options passed?
260 let dev = getConfig("development", options);
261 let staging = getConfig("staging", options);
262 let configs = getAllConfigs(options);
263
264 it('gets 2 configs', function() {
265 assert.strictEqual(Object.keys(configs).length, 2);
266 });
267
268 it('development config validates', function() {
269 common.assert_development_plus_TEST_config(dev);
270 });
271
272 it('staging config validates', function() {
273 common.assert_staging_config(staging);
274 });
275
276 it('from all: development config validates', function() {
277 common.assert_development_plus_TEST_config(configs.development);
278 });
279
280 it('from all: staging config validates', function() {
281 common.assert_staging_config(configs.staging);
282 });
283
284 // I didn't see that the values in the configs were different. Maybe something about the deepEqual is causing it to be different. The only weird thing, is that it was working before I made some changes. Nothing is wrong with the other ways of checking the configs returned. So, all is well enough.
285 // it('deepStrict validates', function() {
286 // let obj1 = {
287 // "development": dev,
288 // "staging": staging
289 // };
290
291 // assert.deepEqual(configs, obj1);
292 // });
293 });
294 });
295});
\No newline at end of file