/
env-or-json-file-spec.js
67 lines (54 loc) · 1.63 KB
/
env-or-json-file-spec.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
'use strict'
const la = require('lazy-ass')
const is = require('check-more-types')
// quick and dirty deep comparison
const isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b)
/* global describe, it, beforeEach, afterEach */
describe('@cypress/env-or-json-file', () => {
const { configFromEnvOrJsonFile } = require('.')
it('is a function', () => {
la(is.fn(configFromEnvOrJsonFile))
})
const config = {
foo: 'bar',
baz: 42
}
const configString = JSON.stringify(config)
it('returns undefined if cannot load', () => {
const loaded = configFromEnvOrJsonFile('does not exist')
la(loaded === undefined, 'loaded something', loaded)
})
describe('loads from env variable', () => {
beforeEach(() => {
process.env.foo_json = configString
})
afterEach(() => {
delete process.env.foo_json
})
it('simple config', () => {
const loaded = configFromEnvOrJsonFile('foo.json')
la(isEqual(loaded, config), 'wrong config', loaded)
})
})
describe('loads from file', () => {
const sinon = require('sinon')
const fs = require('fs')
const path = require('path')
beforeEach(() => {
const fullPath = path.resolve('foo.json')
sinon.stub(fs, 'existsSync').withArgs(fullPath).returns(true)
sinon
.stub(fs, 'readFileSync')
.withArgs(fullPath, 'utf8')
.returns(configString)
})
afterEach(() => {
fs.existsSync.restore()
fs.readFileSync.restore()
})
it('loads foo.json', () => {
const loaded = configFromEnvOrJsonFile('foo.json')
la(isEqual(loaded, config), 'wrong config', loaded)
})
})
})