@@ -11,7 +11,13 @@ const fs = new Filesystem(join(__dirname, 'fixtures'))
1111const viteConfig = { root : fs . basePath }
1212const viteEnvConfig = { mode : 'development' , command : 'serve' } as const
1313
14- test . group ( 'Zod validation adaptater' , ( ) => {
14+ const ENV_FILENAME = '.env.development'
15+
16+ test . group ( 'Zod validation adaptater' , ( group ) => {
17+ group . each . teardown ( async ( ) => {
18+ await fs . cleanup ( )
19+ } )
20+
1521 test ( 'Basic' , async ( { assert } ) => {
1622 assert . plan ( 1 )
1723
@@ -20,7 +26,7 @@ test.group('Zod validation adaptater', () => {
2026 schema : { VITE_TEST : z . string ( ) . url ( ) . max ( 10 ) } ,
2127 } )
2228
23- await fs . add ( `.env.development` , ` VITE_TEST=htest` )
29+ await fs . add ( ENV_FILENAME , ' VITE_TEST=htest' )
2430
2531 try {
2632 // @ts -ignore
@@ -41,9 +47,9 @@ test.group('Zod validation adaptater', () => {
4147 } ,
4248 } )
4349
44- await fs . add ( `.env.development` , ` VITE_TEST=hello` )
50+ await fs . add ( ENV_FILENAME , ' VITE_TEST=hello' )
4551
46- // @ts -expect-error - ` config` is the handler
52+ // @ts -expect-error - ' config' is the handler
4753 await plugin . config ! ( viteConfig , viteEnvConfig )
4854 assert . equal ( process . env . VITE_TEST , 'HELLO' )
4955 } )
@@ -58,7 +64,7 @@ test.group('Zod validation adaptater', () => {
5864 } ,
5965 } )
6066
61- await fs . add ( `.env.development` , ` VITE_LONG_STRING=superlongstring` )
67+ await fs . add ( ENV_FILENAME , ' VITE_LONG_STRING=superlongstring' )
6268
6369 try {
6470 // @ts -ignore
@@ -80,7 +86,7 @@ test.group('Zod validation adaptater', () => {
8086 } ,
8187 } )
8288
83- await fs . add ( `.env.development` , ` VITE_REFINED=superlongstring` )
89+ await fs . add ( ENV_FILENAME , ' VITE_REFINED=superlongstring' )
8490
8591 try {
8692 // @ts -ignore
@@ -101,7 +107,7 @@ test.group('Zod validation adaptater', () => {
101107 } ,
102108 } )
103109
104- await fs . add ( `.env.development` , `` )
110+ await fs . add ( ENV_FILENAME , '' )
105111
106112 try {
107113 // @ts -ignore
@@ -111,4 +117,31 @@ test.group('Zod validation adaptater', () => {
111117 assert . include ( error . message , 'Invalid value for "VITE_B" : Required' )
112118 }
113119 } )
120+
121+ test ( 'Optional Variables' , async ( { assert } ) => {
122+ assert . plan ( 2 )
123+
124+ const plugin = ValidateEnv ( {
125+ validator : 'zod' ,
126+ schema : { VITE_OPTIONAL_ZOD : z . string ( ) . max ( 2 ) . optional ( ) } ,
127+ } )
128+
129+ // Test with the variable set, but invalid
130+ await fs . add ( ENV_FILENAME , 'VITE_OPTIONAL_ZOD=hello' )
131+ try {
132+ // @ts -ignore
133+ await plugin . config ( viteConfig , viteEnvConfig )
134+ } catch ( error : any ) {
135+ assert . include (
136+ error . message ,
137+ 'Invalid value for "VITE_OPTIONAL_ZOD" : String must contain at most 2 character(s)'
138+ )
139+ }
140+
141+ // Test without variable
142+ await fs . add ( ENV_FILENAME , '' )
143+ // @ts -ignore
144+ await plugin . config ( viteConfig , viteEnvConfig )
145+ assert . equal ( process . env . VITE_OPTIONAL_ZOD , undefined )
146+ } )
114147} )
0 commit comments