-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
136 lines (113 loc) · 3.1 KB
/
index.test.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
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
const test = require('ava')
const {
env,
detectEnvironment,
setDetector,
forceEnvironment,
default: dynamicVariables,
} = require('./index')
test('detects frontend/backend environments by default', (t) => {
t.is(process.browser, undefined, 'the tests are not running in the browser')
t.truthy(process.platform, 'the tests are running in a server/docker')
t.is(detectEnvironment(), true, 'it is running in the backend because process.platform is set')
process.platform = 'darwin'
t.is(detectEnvironment(), true, 'it is running in the backend if process.platform is set')
process.browser = true
t.is(detectEnvironment(), false, 'it is running in the frontend if process.browser is true')
process.browser = false
t.is(detectEnvironment(), true, 'it is running in the frontend if process.browser is false')
delete process.browser
delete process.platform
t.is(
detectEnvironment(),
null,
'environment is unknown if both process.platform and process.browser are not set'
)
})
test('detects custom environments', (t) => {
setDetector(() => process.env.ENV)
process.env.ENV = 1
t.is(detectEnvironment(), '1', `it is running in the custom environment '${process.env.ENV}'`)
})
test('return the right variable', (t) => {
let backend
setDetector(() => backend, false)
backend = false
t.is(
env(1, () => 2),
1,
`fetch the primary environment`
)
backend = true
t.is(
env(1, () => 2),
2,
`fetch the secondary environment`
)
forceEnvironment(false)
t.is(
env(1, () => 2),
1,
`fetch the primary environment`
)
})
test('caches environment and return the right variable', (t) => {
let backend
setDetector(() => backend, true)
backend = false
t.is(env(1, 2), 1, `fetch the primary environment`)
backend = true
t.is(env(1, 2), 1, `fetch the primary environment`)
forceEnvironment(true)
t.is(env(1, 2), 2, `fetch the secondary environment`)
})
test('return the right variable under the advanced usage', (t) => {
const options = { BROWSER: 'BROWSER', SSR: 'SSR', BUILD: 'BUILD' }
forceEnvironment(options.BROWSER)
t.is(
env({ [options.BROWSER]: 1, [options.SSR]: 2, [options.BUILD]: 3 }),
1,
`fetch the primary environment`
)
forceEnvironment(options.SSR)
t.is(
env({ [options.BROWSER]: 1, [options.SSR]: 2, [options.BUILD]: 3 }),
2,
`fetch the secondary environment`
)
forceEnvironment(options.BUILD)
t.is(
env({ [options.BROWSER]: 1, [options.SSR]: 2, [options.BUILD]: 3 }),
3,
`fetch the tertiary environment`
)
})
test('execute all proxied functions', (t) => {
const vars = () =>
dynamicVariables({
A: env(() => 1),
B: () => env(0, 2), // env is calculated in runtime
C: {
D: env(3, 0), // env is calculated in build time
E: () => env(4),
},
})
forceEnvironment(true)
t.deepEqual(JSON.parse(JSON.stringify(vars())), {
A: 1,
B: 2,
C: {
D: 0,
E: 4,
},
})
forceEnvironment(false)
t.deepEqual(JSON.parse(JSON.stringify(vars())), {
A: 1,
B: 0,
C: {
D: 3,
E: 4,
},
})
})