@@ -170,6 +170,119 @@ describe("transform mode plugin", () => {
170170 }
171171 } ) ;
172172
173+ it ( "fails during config resolution when the environment is invalid" , async ( ) => {
174+ const root = mkdtempSync ( join ( tmpdir ( ) , "arkenv-vite-invalid-startup-" ) ) ;
175+ temps . push ( root ) ;
176+ writeFileSync (
177+ join ( root , "env.ts" ) ,
178+ 'import arkenv from "@arkenv/core";\n\nexport const env = arkenv({ REQUIRED_TOKEN: "string" });\n' ,
179+ ) ;
180+ const plugin = arkenvPlugin ( ) as any ;
181+ const context = { } as any ;
182+
183+ if ( plugin . config && typeof plugin . config === "function" ) {
184+ plugin . config . call (
185+ context ,
186+ { root, envDir : root } ,
187+ { mode : "test" , command : "serve" } ,
188+ ) ;
189+ }
190+
191+ await expect (
192+ Promise . resolve ( ) . then ( ( ) =>
193+ plugin . configResolved ?. call ( context , {
194+ root,
195+ envDir : root ,
196+ envPrefix : "VITE_" ,
197+ } as any ) ,
198+ ) ,
199+ ) . rejects . toMatchObject ( { name : "ArkEnvError" } ) ;
200+ expect ( process . env . REQUIRED_TOKEN ) . toBeUndefined ( ) ;
201+ } ) ;
202+
203+ it ( "revalidates valid schema and dotenv changes during HMR" , async ( ) => {
204+ const root = mkdtempSync ( join ( tmpdir ( ) , "arkenv-vite-hmr-" ) ) ;
205+ temps . push ( root ) ;
206+ const schemaPath = join ( root , "env.ts" ) ;
207+ const dotenvPath = join ( root , ".env.test" ) ;
208+ writeFileSync (
209+ schemaPath ,
210+ 'import arkenv from "@arkenv/core";\n\nexport const env = arkenv({ VITE_API_URL: "string" });\n' ,
211+ ) ;
212+ writeFileSync ( dotenvPath , "VITE_API_URL=https://example.com\n" ) ;
213+ const plugin = arkenvPlugin ( { schemaPath } ) as any ;
214+ const server = {
215+ moduleGraph : {
216+ getModulesByFile : ( ) => new Set ( [ { id : schemaPath } ] ) ,
217+ invalidateModule : ( ) => { } ,
218+ } ,
219+ } as any ;
220+ const context = { } as any ;
221+
222+ plugin . config ?. call (
223+ context ,
224+ { root, envDir : root } ,
225+ { mode : "test" , command : "serve" } ,
226+ ) ;
227+ await plugin . configResolved ?. call ( context , {
228+ root,
229+ envDir : root ,
230+ envPrefix : "VITE_" ,
231+ } as any ) ;
232+
233+ const schemaUpdate = plugin . handleHotUpdate ?. call ( context , {
234+ file : schemaPath ,
235+ server,
236+ } as any ) ;
237+ expect ( schemaUpdate ) . toHaveLength ( 1 ) ;
238+ const dotenvUpdate = plugin . handleHotUpdate ?. call ( context , {
239+ file : dotenvPath ,
240+ server,
241+ } as any ) ;
242+ expect ( dotenvUpdate ) . toHaveLength ( 1 ) ;
243+ } ) ;
244+
245+ it ( "propagates invalid dotenv values during HMR" , async ( ) => {
246+ const root = mkdtempSync ( join ( tmpdir ( ) , "arkenv-vite-invalid-hmr-" ) ) ;
247+ temps . push ( root ) ;
248+ const schemaPath = join ( root , "env.ts" ) ;
249+ const dotenvPath = join ( root , ".env.test" ) ;
250+ writeFileSync (
251+ schemaPath ,
252+ 'import arkenv from "@arkenv/core";\n\nexport const env = arkenv({ VITE_PORT: "number" });\n' ,
253+ ) ;
254+ writeFileSync ( dotenvPath , "VITE_PORT=8080\n" ) ;
255+ const plugin = arkenvPlugin ( { schemaPath } ) as any ;
256+ const server = {
257+ moduleGraph : {
258+ getModulesByFile : ( ) => new Set ( ) ,
259+ invalidateModule : ( ) => { } ,
260+ } ,
261+ } as any ;
262+ const context = { } as any ;
263+
264+ plugin . config ?. call (
265+ context ,
266+ { root, envDir : root } ,
267+ { mode : "test" , command : "serve" } ,
268+ ) ;
269+ await plugin . configResolved ?. call ( context , {
270+ root,
271+ envDir : root ,
272+ envPrefix : "VITE_" ,
273+ } as any ) ;
274+ writeFileSync ( dotenvPath , "VITE_PORT=not-a-number\n" ) ;
275+
276+ await expect (
277+ Promise . resolve ( ) . then ( ( ) =>
278+ plugin . handleHotUpdate ?. call ( context , {
279+ file : dotenvPath ,
280+ server,
281+ } as any ) ,
282+ ) ,
283+ ) . rejects . toMatchObject ( { name : "ArkEnvError" } ) ;
284+ } ) ;
285+
173286 it ( "passes through the env module unchanged in the SSR graph" , async ( ) => {
174287 const fixtureDir = join ( __dirname , "__fixtures__" , "transform-env" ) ;
175288 const plugin = arkenvPlugin ( { schemaPath : join ( fixtureDir , "env.ts" ) } ) ;
0 commit comments