-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathenv.spec.ts
More file actions
337 lines (283 loc) · 9.48 KB
/
env.spec.ts
File metadata and controls
337 lines (283 loc) · 9.48 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const ALL_ENV_VARS = [
'CONTEXT',
'VERCEL_ENV',
'URL',
'NUXT_ENV_VERCEL_URL',
'NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL',
'PULL_REQUEST',
'VERCEL_GIT_PULL_REQUEST_ID',
'BRANCH',
'VERCEL_GIT_COMMIT_REF',
]
describe('isCanary', () => {
beforeEach(() => {
vi.resetModules()
})
beforeEach(() => {
for (const envVar of ALL_ENV_VARS) {
vi.stubEnv(envVar, undefined)
}
})
afterEach(() => {
vi.unstubAllEnvs()
})
it('returns true when VERCEL_ENV is "production" and branch is "main"', async () => {
vi.stubEnv('VERCEL_ENV', 'production')
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'main')
const { isCanary } = await import('../../../config/env')
expect(isCanary).toBe(true)
})
it('returns true when VERCEL_ENV is "preview" and branch is "main" (non-PR)', async () => {
vi.stubEnv('VERCEL_ENV', 'preview')
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'main')
const { isCanary } = await import('../../../config/env')
expect(isCanary).toBe(true)
})
it('returns true when VERCEL_ENV is custom "canary" and branch is "main"', async () => {
vi.stubEnv('VERCEL_ENV', 'canary')
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'main')
const { isCanary } = await import('../../../config/env')
expect(isCanary).toBe(true)
})
it('returns false when VERCEL_ENV is "preview", branch is "main", but is a PR', async () => {
vi.stubEnv('VERCEL_ENV', 'preview')
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'main')
vi.stubEnv('VERCEL_GIT_PULL_REQUEST_ID', '123')
const { isCanary } = await import('../../../config/env')
expect(isCanary).toBe(false)
})
it.each([
['production (non-main branch)', 'production', 'v1.0.0'],
['preview (non-main branch)', 'preview', 'feat/foo'],
['development', 'development', undefined],
['unset', undefined, undefined],
])('returns false when VERCEL_ENV is %s', async (_label, value, branch) => {
if (value !== undefined) vi.stubEnv('VERCEL_ENV', value)
if (branch !== undefined) vi.stubEnv('VERCEL_GIT_COMMIT_REF', branch)
const { isCanary } = await import('../../../config/env')
expect(isCanary).toBe(false)
})
})
describe('getEnv', () => {
beforeEach(() => {
vi.resetModules()
})
beforeEach(() => {
for (const envVar of ALL_ENV_VARS) {
vi.stubEnv(envVar, undefined)
}
})
afterEach(() => {
vi.unstubAllEnvs()
})
it('returns "dev" in development mode', async () => {
const { getEnv } = await import('../../../config/env')
const result = await getEnv(true)
expect(result.env).toBe('dev')
})
it('returns "canary" for Vercel preview deploys from main branch (non-PR)', async () => {
vi.stubEnv('VERCEL_ENV', 'preview')
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'main')
const { getEnv } = await import('../../../config/env')
const result = await getEnv(false)
expect(result.env).toBe('canary')
})
it('returns "canary" for custom Vercel "canary" environment on main branch', async () => {
vi.stubEnv('VERCEL_ENV', 'canary')
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'main')
const { getEnv } = await import('../../../config/env')
const result = await getEnv(false)
expect(result.env).toBe('canary')
})
it('returns "preview" for Vercel preview PR deploys', async () => {
vi.stubEnv('VERCEL_ENV', 'preview')
vi.stubEnv('VERCEL_GIT_PULL_REQUEST_ID', '123')
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'feat/foo')
const { getEnv } = await import('../../../config/env')
const result = await getEnv(false)
expect(result.env).toBe('preview')
})
it('returns "preview" for PR deploys from main branch', async () => {
vi.stubEnv('VERCEL_ENV', 'preview')
vi.stubEnv('VERCEL_GIT_PULL_REQUEST_ID', '456')
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'main')
const { getEnv } = await import('../../../config/env')
const result = await getEnv(false)
expect(result.env).toBe('preview')
})
it('returns "canary" for Vercel production deploys from main branch', async () => {
vi.stubEnv('VERCEL_ENV', 'production')
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'main')
const { getEnv } = await import('../../../config/env')
const result = await getEnv(false)
expect(result.env).toBe('canary')
})
it('returns "release" for Vercel production deploys from non-main branch', async () => {
vi.stubEnv('VERCEL_ENV', 'production')
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'v1.0.0')
const { getEnv } = await import('../../../config/env')
const result = await getEnv(false)
expect(result.env).toBe('release')
})
it('prioritises "dev" over "canary" in development mode', async () => {
vi.stubEnv('VERCEL_ENV', 'preview')
vi.stubEnv('VERCEL_GIT_COMMIT_REF', 'main')
const { getEnv } = await import('../../../config/env')
const result = await getEnv(true)
expect(result.env).toBe('dev')
})
})
describe('getPreviewUrl', () => {
beforeEach(() => {
// Reset consts evaluated at module init time
vi.resetModules()
})
beforeEach(() => {
for (const envVar of ALL_ENV_VARS) {
vi.stubEnv(envVar, undefined)
}
})
afterEach(() => {
vi.unstubAllEnvs()
})
it('returns `undefined` if no known preview env is detected', async () => {
const { getPreviewUrl } = await import('../../../config/env')
expect(getPreviewUrl()).toBeUndefined()
})
it.each([
['Netlify production', { CONTEXT: 'production', URL: 'https://prod.example.com' }],
['Vercel production', { VERCEL_ENV: 'production', NUXT_ENV_VERCEL_URL: 'prod.example.com' }],
])('%s environment returns `undefined`', async (_name, envVars) => {
for (const [key, value] of Object.entries(envVars)) {
vi.stubEnv(key, value)
}
const { getPreviewUrl } = await import('../../../config/env')
expect(getPreviewUrl()).toBeUndefined()
})
it.each([
['Netlify dev', { CONTEXT: 'dev', URL: 'https://dev.example.com' }, 'https://dev.example.com'],
[
'Netlify deploy-preview',
{
CONTEXT: 'deploy-preview',
URL: 'https://preview.example.com',
},
'https://preview.example.com',
],
[
'Netlify branch-deploy',
{ CONTEXT: 'branch-deploy', URL: 'https://beta.example.com' },
'https://beta.example.com',
],
[
'Netlify preview-server',
{
CONTEXT: 'preview-server',
URL: 'https://my-feat--preview.example.com',
},
'https://my-feat--preview.example.com',
],
[
'Vercel development',
{ VERCEL_ENV: 'development', NUXT_ENV_VERCEL_URL: 'dev.example.com' },
'https://dev.example.com',
],
[
'Vercel preview',
{ VERCEL_ENV: 'preview', NUXT_ENV_VERCEL_URL: 'preview.example.com' },
'https://preview.example.com',
],
])('%s environment returns preview URL', async (_name, envVars, expectedUrl) => {
for (const [key, value] of Object.entries(envVars)) {
vi.stubEnv(key, value)
}
const { getPreviewUrl } = await import('../../../config/env')
expect(getPreviewUrl()).toBe(expectedUrl)
})
})
describe('getProductionUrl', () => {
beforeEach(() => {
// Reset consts evaluated at module init time
vi.resetModules()
})
beforeEach(() => {
for (const envVar of ALL_ENV_VARS) {
vi.stubEnv(envVar, undefined)
}
})
afterEach(() => {
vi.unstubAllEnvs()
})
it('returns `undefined` if no known production env is detected', async () => {
const { getProductionUrl } = await import('../../../config/env')
expect(getProductionUrl()).toBeUndefined()
})
it.each([
['Netlify dev', { CONTEXT: 'dev', URL: 'https://dev.example.com' }],
[
'Netlify deploy-preview',
{
CONTEXT: 'deploy-preview',
URL: 'https://preview.example.com',
},
],
['Netlify branch-deploy', { CONTEXT: 'branch-deploy', URL: 'https://beta.example.com' }],
[
'Netlify preview-server',
{
CONTEXT: 'preview-server',
URL: 'https://my-feat--preview.example.com',
},
],
[
'Vercel development',
{
VERCEL_ENV: 'development',
NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL: 'dev.example.com',
},
],
[
'Vercel preview',
{
VERCEL_ENV: 'preview',
NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL: 'preview.example.com',
},
],
])('%s environment returns `undefined`', async (_name, envVars) => {
for (const [key, value] of Object.entries(envVars)) {
vi.stubEnv(key, value)
}
const { getProductionUrl } = await import('../../../config/env')
expect(getProductionUrl()).toBeUndefined()
})
it.each([
[
'Netlify production',
{ CONTEXT: 'production', URL: 'https://prod.example.com' },
'https://prod.example.com',
],
[
'Vercel production',
{
VERCEL_ENV: 'production',
NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL: 'prod.example.com',
},
'https://prod.example.com',
],
])('%s environment returns production URL', async (_name, envVars, expectedUrl) => {
for (const [key, value] of Object.entries(envVars)) {
vi.stubEnv(key, value)
}
const { getProductionUrl } = await import('../../../config/env')
expect(getProductionUrl()).toBe(expectedUrl)
})
})
describe('getVersion', () => {
it('returns a valid semver string without a leading "v"', async () => {
const { getVersion } = await import('../../../config/env')
const result = await getVersion()
expect(result).not.toMatch(/^v/)
expect(result).toMatch(/^\d+\.\d+\.\d+$/)
})
})