Skip to content

Commit 3e2284b

Browse files
committed
largely remove mocking from tests except where useful
1 parent 2039c4e commit 3e2284b

File tree

1 file changed

+74
-59
lines changed

1 file changed

+74
-59
lines changed

tests/test-config.js

+74-59
Original file line numberDiff line numberDiff line change
@@ -1,169 +1,182 @@
11
const fs = require('fs')
2-
// const os = require('os')
3-
// const path = require('path')
2+
const os = require('os')
3+
const path = require('path')
44

55
const sinon = require('sinon')
66
const t = require('tap')
77

88
const dotenv = require('../lib/main')
99

10-
const mockParseResponse = { test: 'foo' }
10+
t.beforeEach(() => {
11+
delete process.env.BASIC // reset
12+
})
1113

1214
t.test('takes string for path option', ct => {
13-
ct.plan(2)
14-
1515
const testPath = 'tests/.env'
1616
const env = dotenv.config({ path: testPath })
1717

1818
ct.equal(env.parsed.BASIC, 'basic')
1919
ct.equal(process.env.BASIC, 'basic')
20+
21+
ct.end()
2022
})
2123

2224
t.test('takes array for path option', ct => {
23-
ct.plan(2)
24-
2525
const testPath = ['tests/.env']
2626
const env = dotenv.config({ path: testPath })
2727

2828
ct.equal(env.parsed.BASIC, 'basic')
2929
ct.equal(process.env.BASIC, 'basic')
30+
31+
ct.end()
3032
})
3133

3234
t.test('takes two or more files in the array for path option', ct => {
33-
ct.plan(2)
34-
3535
const testPath = ['tests/.env.local', 'tests/.env']
3636
const env = dotenv.config({ path: testPath })
3737

3838
ct.equal(env.parsed.BASIC, 'local_basic')
3939
ct.equal(process.env.BASIC, 'local_basic')
40+
41+
ct.end()
4042
})
4143

4244
t.test('takes URL for path option', ct => {
43-
ct.plan(2)
45+
const envPath = path.resolve(__dirname, '.env')
46+
const fileUrl = new URL(`file://${envPath}`)
4447

45-
const testPath = new URL('file://home/user/project/.env')
46-
const env = dotenv.config({ path: testPath })
48+
const env = dotenv.config({ path: fileUrl })
4749

4850
ct.equal(env.parsed.BASIC, 'basic')
4951
ct.equal(process.env.BASIC, 'basic')
52+
53+
ct.end()
5054
})
5155

52-
// t.test('takes option for path along with home directory char ~', ct => {
53-
// ct.plan(2)
54-
// const mockedHomedir = '/Users/dummy'
55-
// const homedirStub = sinon.stub(os, 'homedir').returns(mockedHomedir)
56-
// const testPath = '~/.env'
57-
// dotenv.config({ path: testPath })
58-
//
59-
// ct.equal(readFileSyncStub.args[0][0], path.join(mockedHomedir, '.env'))
60-
// ct.ok(homedirStub.called)
61-
// homedirStub.restore()
62-
// })
56+
t.test('takes option for path along with home directory char ~', ct => {
57+
const readFileSyncStub = sinon.stub(fs, 'readFileSync').returns('test=foo')
58+
const mockedHomedir = '/Users/dummy'
59+
const homedirStub = sinon.stub(os, 'homedir').returns(mockedHomedir)
60+
const testPath = '~/.env'
61+
dotenv.config({ path: testPath })
6362

64-
t.test('takes option for encoding', ct => {
65-
ct.plan(1)
63+
ct.equal(readFileSyncStub.args[0][0], path.join(mockedHomedir, '.env'))
64+
ct.ok(homedirStub.called)
6665

67-
const testEncoding = 'latin1'
68-
dotenv.config({ encoding: testEncoding })
66+
homedirStub.restore()
67+
readFileSyncStub.restore()
68+
ct.end()
69+
})
6970

71+
t.test('takes option for encoding', ct => {
7072
const readFileSyncStub = sinon.stub(fs, 'readFileSync').returns('test=foo')
7173

74+
const testEncoding = 'latin1'
75+
dotenv.config({ encoding: testEncoding })
7276
ct.equal(readFileSyncStub.args[0][1].encoding, testEncoding)
77+
7378
readFileSyncStub.restore()
79+
ct.end()
7480
})
7581

7682
t.test('takes option for debug', ct => {
77-
ct.plan(1)
78-
7983
const logStub = sinon.stub(console, 'log')
80-
dotenv.config({ debug: 'true' })
8184

85+
dotenv.config({ debug: 'true' })
8286
ct.ok(logStub.called)
87+
8388
logStub.restore()
89+
ct.end()
8490
})
8591

8692
t.test('reads path with encoding, parsing output to process.env', ct => {
87-
ct.plan(2)
93+
const readFileSyncStub = sinon.stub(fs, 'readFileSync').returns('BASIC=basic')
94+
const parseStub = sinon.stub(dotenv, 'parse').returns({ BASIC: 'basic' })
8895

89-
const env = dotenv.config()
96+
const res = dotenv.config()
9097

91-
ct.same(env.parsed.BASIC, 'basic')
92-
ct.same(process.env.BASIC, 'basic')
98+
ct.same(res.parsed, { BASIC: 'basic' })
99+
ct.equal(readFileSyncStub.callCount, 1)
100+
101+
readFileSyncStub.restore()
102+
parseStub.restore()
103+
104+
ct.end()
93105
})
94106

95107
t.test('does not write over keys already in process.env', ct => {
96-
ct.plan(2)
97-
108+
const testPath = 'tests/.env'
98109
const existing = 'bar'
99110
process.env.BASIC = existing
100-
const env = dotenv.config()
111+
const env = dotenv.config({ path: testPath })
101112

102113
ct.equal(env.parsed.BASIC, 'basic')
103114
ct.equal(process.env.BASIC, existing)
115+
116+
ct.end()
104117
})
105118

106119
t.test('does write over keys already in process.env if override turned on', ct => {
107-
ct.plan(2)
108-
120+
const testPath = 'tests/.env'
109121
const existing = 'bar'
110122
process.env.BASIC = existing
111-
const env = dotenv.config({ override: true })
123+
const env = dotenv.config({ path: testPath, override: true })
112124

113125
ct.equal(env.parsed.BASIC, 'basic')
114126
ct.equal(process.env.BASIC, 'basic')
127+
128+
ct.end()
115129
})
116130

117131
t.test('does not write over keys already in process.env if the key has a falsy value', ct => {
118-
ct.plan(2)
119-
132+
const testPath = 'tests/.env'
120133
const existing = ''
121134
process.env.BASIC = existing
122-
const env = dotenv.config()
135+
const env = dotenv.config({ path: testPath })
123136

124137
ct.equal(env.parsed.BASIC, 'basic')
125-
ct.equal(process.env.BASIC, undefined)
138+
ct.equal(process.env.BASIC, '')
139+
140+
ct.end()
126141
})
127142

128143
t.test('does write over keys already in process.env if the key has a falsy value but override is set to true', ct => {
129-
ct.plan(2)
130-
144+
const testPath = 'tests/.env'
131145
const existing = ''
132146
process.env.BASIC = existing
133-
// 'foo' returned as value in `beforeEach`. should keep this ''
134-
const env = dotenv.config({ override: true })
147+
const env = dotenv.config({ path: testPath, override: true })
135148

136149
ct.equal(env.parsed.BASIC, 'basic')
137-
ct.equal(process.env.BASIC, '')
138-
// ct.ok(process.env.test)
150+
ct.equal(process.env.BASIC, 'basic')
151+
ct.end()
139152
})
140153

141154
t.test('can write to a different object rather than process.env', ct => {
142-
ct.plan(3)
143-
155+
const testPath = 'tests/.env'
144156
process.env.BASIC = 'other' // reset process.env
145157

146158
const myObject = {}
147-
const env = dotenv.config({ processEnv: myObject })
159+
const env = dotenv.config({ path: testPath, processEnv: myObject })
148160

149161
ct.equal(env.parsed.BASIC, 'basic')
150162
console.log('logging', process.env.BASIC)
151163
ct.equal(process.env.BASIC, 'other')
152164
ct.equal(myObject.BASIC, 'basic')
165+
166+
ct.end()
153167
})
154168

155169
t.test('returns parsed object', ct => {
156-
ct.plan(2)
157-
158-
const env = dotenv.config()
170+
const testPath = 'tests/.env'
171+
const env = dotenv.config({ path: testPath })
159172

160173
ct.notOk(env.error)
161-
ct.same(env.parsed, mockParseResponse)
174+
ct.equal(env.parsed.BASIC, 'basic')
175+
176+
ct.end()
162177
})
163178

164179
t.test('returns any errors thrown from reading file or parsing', ct => {
165-
ct.plan(1)
166-
167180
const readFileSyncStub = sinon.stub(fs, 'readFileSync').returns('test=foo')
168181

169182
readFileSyncStub.throws()
@@ -172,6 +185,8 @@ t.test('returns any errors thrown from reading file or parsing', ct => {
172185
ct.type(env.error, Error)
173186

174187
readFileSyncStub.restore()
188+
189+
ct.end()
175190
})
176191

177192
t.test('logs any errors thrown from reading file or parsing when in debug mode', ct => {

0 commit comments

Comments
 (0)