-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathenv.spec.ts
More file actions
173 lines (153 loc) · 4.52 KB
/
env.spec.ts
File metadata and controls
173 lines (153 loc) · 4.52 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
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',
]
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: 'branch-deploy',
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)
})
})