Skip to content

Commit 76f2e7b

Browse files
committed
test: add unit tests for getPreviewUrl, getProductionUrl
1 parent 6b24e28 commit 76f2e7b

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed

test/unit/config/env.spec.ts

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2+
3+
const ALL_ENV_VARS = [
4+
'CONTEXT',
5+
'VERCEL_ENV',
6+
'NUXT_ENV_URL',
7+
'NUXT_ENV_VERCEL_URL',
8+
'NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL',
9+
]
10+
11+
describe('getPreviewUrl', () => {
12+
beforeEach(() => {
13+
// Reset consts evaluated at module init time
14+
vi.resetModules()
15+
})
16+
17+
beforeEach(() => {
18+
for (const envVar of ALL_ENV_VARS) {
19+
vi.stubEnv(envVar, undefined)
20+
}
21+
})
22+
23+
afterEach(() => {
24+
vi.unstubAllEnvs()
25+
})
26+
27+
it('returns `undefined` if no known preview env is detected', async () => {
28+
const { getPreviewUrl } = await import('../../../config/env')
29+
30+
expect(getPreviewUrl()).toBeUndefined()
31+
})
32+
33+
it.each([
34+
['Netlify production', { CONTEXT: 'production', NUXT_ENV_URL: 'https://prod.example.com' }],
35+
['Vercel production', { VERCEL_ENV: 'production', NUXT_ENV_VERCEL_URL: 'prod.example.com' }],
36+
])('%s environment returns `undefined`', async (_name, envVars) => {
37+
for (const [key, value] of Object.entries(envVars)) {
38+
vi.stubEnv(key, value)
39+
}
40+
const { getPreviewUrl } = await import('../../../config/env')
41+
42+
expect(getPreviewUrl()).toBeUndefined()
43+
})
44+
45+
it.each([
46+
[
47+
'Netlify dev',
48+
{ CONTEXT: 'dev', NUXT_ENV_URL: 'https://dev.example.com' },
49+
'https://dev.example.com',
50+
],
51+
[
52+
'Netlify deploy-preview',
53+
{
54+
CONTEXT: 'deploy-preview',
55+
NUXT_ENV_URL: 'https://preview.example.com',
56+
},
57+
'https://preview.example.com',
58+
],
59+
[
60+
'Netlify branch-deploy',
61+
{ CONTEXT: 'branch-deploy', NUXT_ENV_URL: 'https://beta.example.com' },
62+
'https://beta.example.com',
63+
],
64+
[
65+
'Netlify preview-server',
66+
{
67+
CONTEXT: 'branch-deploy',
68+
NUXT_ENV_URL: 'https://my-feat--preview.example.com',
69+
},
70+
'https://my-feat--preview.example.com',
71+
],
72+
[
73+
'Vercel development',
74+
{ VERCEL_ENV: 'development', NUXT_ENV_VERCEL_URL: 'dev.example.com' },
75+
'https://dev.example.com',
76+
],
77+
[
78+
'Vercel preview',
79+
{ VERCEL_ENV: 'preview', NUXT_ENV_VERCEL_URL: 'preview.example.com' },
80+
'https://preview.example.com',
81+
],
82+
])('%s environment returns preview URL', async (_name, envVars, expectedUrl) => {
83+
for (const [key, value] of Object.entries(envVars)) {
84+
vi.stubEnv(key, value)
85+
}
86+
87+
const { getPreviewUrl } = await import('../../../config/env')
88+
89+
expect(getPreviewUrl()).toBe(expectedUrl)
90+
})
91+
})
92+
93+
describe('getProductionUrl', () => {
94+
beforeEach(() => {
95+
// Reset consts evaluated at module init time
96+
vi.resetModules()
97+
})
98+
99+
beforeEach(() => {
100+
for (const envVar of ALL_ENV_VARS) {
101+
vi.stubEnv(envVar, undefined)
102+
}
103+
})
104+
105+
afterEach(() => {
106+
vi.unstubAllEnvs()
107+
})
108+
109+
it('returns `undefined` if no known production env is detected', async () => {
110+
const { getProductionUrl } = await import('../../../config/env')
111+
112+
expect(getProductionUrl()).toBeUndefined()
113+
})
114+
115+
it.each([
116+
['Netlify dev', { CONTEXT: 'dev', NUXT_ENV_URL: 'https://dev.example.com' }],
117+
[
118+
'Netlify deploy-preview',
119+
{
120+
CONTEXT: 'deploy-preview',
121+
NUXT_ENV_URL: 'https://preview.example.com',
122+
},
123+
],
124+
[
125+
'Netlify branch-deploy',
126+
{ CONTEXT: 'branch-deploy', NUXT_ENV_URL: 'https://beta.example.com' },
127+
],
128+
[
129+
'Netlify preview-server',
130+
{
131+
CONTEXT: 'preview-server',
132+
NUXT_ENV_URL: 'https://my-feat--preview.example.com',
133+
},
134+
],
135+
[
136+
'Vercel development',
137+
{
138+
VERCEL_ENV: 'development',
139+
NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL: 'dev.example.com',
140+
},
141+
],
142+
[
143+
'Vercel preview',
144+
{
145+
VERCEL_ENV: 'preview',
146+
NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL: 'preview.example.com',
147+
},
148+
],
149+
])('%s environment returns `undefined`', async (_name, envVars) => {
150+
for (const [key, value] of Object.entries(envVars)) {
151+
vi.stubEnv(key, value)
152+
}
153+
const { getProductionUrl } = await import('../../../config/env')
154+
155+
expect(getProductionUrl()).toBeUndefined()
156+
})
157+
158+
it.each([
159+
[
160+
'Netlify production',
161+
{ CONTEXT: 'production', NUXT_ENV_URL: 'https://prod.example.com' },
162+
'https://prod.example.com',
163+
],
164+
[
165+
'Vercel production',
166+
{
167+
VERCEL_ENV: 'production',
168+
NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL: 'prod.example.com',
169+
},
170+
'https://prod.example.com',
171+
],
172+
])('%s environment returns production URL', async (_name, envVars, expectedUrl) => {
173+
for (const [key, value] of Object.entries(envVars)) {
174+
vi.stubEnv(key, value)
175+
}
176+
const { getProductionUrl } = await import('../../../config/env')
177+
178+
expect(getProductionUrl()).toBe(expectedUrl)
179+
})
180+
})

0 commit comments

Comments
 (0)