-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure-local.tests.ts
144 lines (98 loc) · 4.45 KB
/
configure-local.tests.ts
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import CHAI = require('chai')
const expect = CHAI.expect
describe('configure-local', function() {
process.env.NODE_ENV = 'development'
var configure = require('configure-local')
function scopeEnvVars(env: {}, test: () => void) {
var saved = process.env
process.env = env
test()
process.env = saved
}
function scopeArgs(args: string[], test: () => void) {
var saved = process.argv
process.argv = args.concat(args)
test()
process.argv = saved
}
describe('settings and overrides', function() {
describe('using config files only', function() {
it('should respect setting specified only in config/common.json', function() {
expect(configure.get('external_service:api_key')).to.equal('0a1b2c3d')
})
it('should respect setting specified only in config/development.json', function() {
expect(configure.get('PORT')).to.equal(1001)
})
it('should override setting in config/common.json with one from config/development.json', function() {
expect(configure.get('external_service:url')).to.equal('https://external_service.co/api/v2/')
})
})
describe('using environment variables', function() {
it('should override a setting in config/common.json', function() {
scopeEnvVars({NODE_ENV: 'development', 'external_service:api_key': 'key_for_testing'}, () => {
configure.test.reset()
expect(configure.get('external_service:api_key')).to.equal('key_for_testing')
})
})
it('should override a setting in config/development.json', function() {
scopeEnvVars({NODE_ENV: 'development', PORT: '3333'}, () => {
configure.test.reset()
expect(configure.get('PORT')).to.equal('3333')
})
})
})
describe('using command line arguments', function() {
it('should override a setting in config/development.json', function() {
scopeArgs(['--PORT', '8888'], function() {
configure.test.reset()
expect(configure.get('PORT')).to.equal(8888)
})
})
it('should override a setting in environment variables', function() {
scopeEnvVars({NODE_ENV: 'development', PORT: '3333'}, () => {
scopeArgs(['--PORT', '8888'], function() {
configure.test.reset()
expect(configure.get('PORT')).to.equal(8888)
})
})
})
})
describe('using defaults', function() {
it('should return default when not overridden', function() {
expect(configure.get('CONFIG_DIR')).to.equal('config')
})
it('should be overridden by environment variable', function(done) {
process.env.CONFIG_DIR = '/var/config'
try {
// test.reset() throws an error because the config file doesnt exist
configure.test.reset()
} catch (error) {
expect(error.message).to.equal("ENOENT: no such file or directory, access '/var/config/development.json'")
done()
}
var env = process.env
delete env['CONFIG_DIR']
})
})
})
describe('Configuration selection by instance environment name', function() {
it('should throw an error if NODE_ENV is unset', function() {
var saved = process.env.NODE_ENV
process.env.NODE_ENV = undefined
expect(configure.test.reset).to.throw(Error)
process.env.NODE_ENV = saved
})
it('should throw an error if NODE_ENV doesnt match a config file', function() {
var saved = process.env.NODE_ENV
process.env.NODE_ENV = 'devel'
expect(configure.test.reset).to.throw(Error)
process.env.NODE_ENV = saved
})
it('should select production when NODE_ENV=production', function() {
scopeEnvVars({NODE_ENV: 'production'}, () => {
configure.test.reset()
expect(configure.get('PORT')).to.equal(2002)
})
})
})
})