@@ -6,8 +6,126 @@ const ALL_ENV_VARS = [
66 'URL' ,
77 'NUXT_ENV_VERCEL_URL' ,
88 'NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL' ,
9+ 'PULL_REQUEST' ,
10+ 'VERCEL_GIT_PULL_REQUEST_ID' ,
11+ 'BRANCH' ,
12+ 'VERCEL_GIT_COMMIT_REF' ,
913]
1014
15+ describe ( 'isCanary' , ( ) => {
16+ beforeEach ( ( ) => {
17+ vi . resetModules ( )
18+ } )
19+
20+ beforeEach ( ( ) => {
21+ for ( const envVar of ALL_ENV_VARS ) {
22+ vi . stubEnv ( envVar , undefined )
23+ }
24+ } )
25+
26+ afterEach ( ( ) => {
27+ vi . unstubAllEnvs ( )
28+ } )
29+
30+ it ( 'returns true when VERCEL_ENV is "canary"' , async ( ) => {
31+ vi . stubEnv ( 'VERCEL_ENV' , 'canary' )
32+ const { isCanary } = await import ( '../../../config/env' )
33+
34+ expect ( isCanary ) . toBe ( true )
35+ } )
36+
37+ it . each ( [
38+ [ 'production' , 'production' ] ,
39+ [ 'preview' , 'preview' ] ,
40+ [ 'development' , 'development' ] ,
41+ [ 'unset' , undefined ] ,
42+ ] ) ( 'returns false when VERCEL_ENV is %s' , async ( _label , value ) => {
43+ if ( value !== undefined ) vi . stubEnv ( 'VERCEL_ENV' , value )
44+ const { isCanary } = await import ( '../../../config/env' )
45+
46+ expect ( isCanary ) . toBe ( false )
47+ } )
48+ } )
49+
50+ describe ( 'getEnv' , ( ) => {
51+ beforeEach ( ( ) => {
52+ vi . resetModules ( )
53+ } )
54+
55+ beforeEach ( ( ) => {
56+ for ( const envVar of ALL_ENV_VARS ) {
57+ vi . stubEnv ( envVar , undefined )
58+ }
59+ } )
60+
61+ afterEach ( ( ) => {
62+ vi . unstubAllEnvs ( )
63+ } )
64+
65+ it ( 'returns "dev" in development mode' , async ( ) => {
66+ const { getEnv } = await import ( '../../../config/env' )
67+ const result = await getEnv ( true )
68+
69+ expect ( result . env ) . toBe ( 'dev' )
70+ } )
71+
72+ it ( 'returns "canary" when VERCEL_ENV is "canary"' , async ( ) => {
73+ vi . stubEnv ( 'VERCEL_ENV' , 'canary' )
74+ vi . stubEnv ( 'VERCEL_GIT_COMMIT_REF' , 'main' )
75+ const { getEnv } = await import ( '../../../config/env' )
76+ const result = await getEnv ( false )
77+
78+ expect ( result . env ) . toBe ( 'canary' )
79+ } )
80+
81+ it ( 'returns "preview" for Vercel preview deploys' , async ( ) => {
82+ vi . stubEnv ( 'VERCEL_ENV' , 'preview' )
83+ vi . stubEnv ( 'VERCEL_GIT_PULL_REQUEST_ID' , '123' )
84+ vi . stubEnv ( 'VERCEL_GIT_COMMIT_REF' , 'main' )
85+ const { getEnv } = await import ( '../../../config/env' )
86+ const result = await getEnv ( false )
87+
88+ expect ( result . env ) . toBe ( 'preview' )
89+ } )
90+
91+ it ( 'returns "preview" for PR deploys from main branch' , async ( ) => {
92+ vi . stubEnv ( 'VERCEL_ENV' , 'preview' )
93+ vi . stubEnv ( 'VERCEL_GIT_PULL_REQUEST_ID' , '456' )
94+ vi . stubEnv ( 'VERCEL_GIT_COMMIT_REF' , 'main' )
95+ const { getEnv } = await import ( '../../../config/env' )
96+ const result = await getEnv ( false )
97+
98+ expect ( result . env ) . toBe ( 'preview' )
99+ } )
100+
101+ it ( 'returns "release" for Vercel production deploys' , async ( ) => {
102+ vi . stubEnv ( 'VERCEL_ENV' , 'production' )
103+ vi . stubEnv ( 'VERCEL_GIT_COMMIT_REF' , 'v1.0.0' )
104+ const { getEnv } = await import ( '../../../config/env' )
105+ const result = await getEnv ( false )
106+
107+ expect ( result . env ) . toBe ( 'release' )
108+ } )
109+
110+ it ( 'prioritises "canary" over "preview" when VERCEL_ENV is "canary" and PR is open' , async ( ) => {
111+ vi . stubEnv ( 'VERCEL_ENV' , 'canary' )
112+ vi . stubEnv ( 'VERCEL_GIT_PULL_REQUEST_ID' , '789' )
113+ vi . stubEnv ( 'VERCEL_GIT_COMMIT_REF' , 'main' )
114+ const { getEnv } = await import ( '../../../config/env' )
115+ const result = await getEnv ( false )
116+
117+ expect ( result . env ) . toBe ( 'canary' )
118+ } )
119+
120+ it ( 'prioritises "dev" over "canary" in development mode' , async ( ) => {
121+ vi . stubEnv ( 'VERCEL_ENV' , 'canary' )
122+ const { getEnv } = await import ( '../../../config/env' )
123+ const result = await getEnv ( true )
124+
125+ expect ( result . env ) . toBe ( 'dev' )
126+ } )
127+ } )
128+
11129describe ( 'getPreviewUrl' , ( ) => {
12130 beforeEach ( ( ) => {
13131 // Reset consts evaluated at module init time
@@ -33,6 +151,7 @@ describe('getPreviewUrl', () => {
33151 it . each ( [
34152 [ 'Netlify production' , { CONTEXT : 'production' , URL : 'https://prod.example.com' } ] ,
35153 [ 'Vercel production' , { VERCEL_ENV : 'production' , NUXT_ENV_VERCEL_URL : 'prod.example.com' } ] ,
154+ [ 'Vercel canary' , { VERCEL_ENV : 'canary' , NUXT_ENV_VERCEL_URL : 'main.example.com' } ] ,
36155 ] ) ( '%s environment returns `undefined`' , async ( _name , envVars ) => {
37156 for ( const [ key , value ] of Object . entries ( envVars ) ) {
38157 vi . stubEnv ( key , value )
0 commit comments