@@ -24,10 +24,13 @@ import {
2424 MALFORMED_HTTP_RESPONSE_ERROR ,
2525} from '../errors/errorTypes' ;
2626import * as errorHandler from '../errors/errorHandler' ;
27- import { describe , it , expect , vi } from 'vitest' ;
27+ import { describe , it , expect , vi , afterEach } from 'vitest' ;
2828
2929vi . mock ( '../errors/errorHandler' ) ;
3030
31+ // Preserve import.meta.env to restore after each test
32+ const ORIGINAL_ENV = { ...import . meta. env } ;
33+
3134// mockServerJson configures an HttpOkResponse containing the given `json`
3235// for all requests made against the given `endpoint`.
3336const mockServerJson = ( endpoint : string , json : object , status : number = 200 ) => {
@@ -36,6 +39,20 @@ const mockServerJson = (endpoint: string, json: object, status: number = 200) =>
3639} ;
3740
3841describe ( 'api/http' , ( ) => {
42+ afterEach ( ( ) => {
43+ // Restore import.meta.env after each test to prevent leakage
44+ // First, delete any keys that weren't in the original
45+ for ( const key in import . meta. env ) {
46+ if ( ! ( key in ORIGINAL_ENV ) ) {
47+ delete import . meta. env [ key ] ;
48+ }
49+ }
50+ // Then restore the original values
51+ Object . assign ( import . meta. env , ORIGINAL_ENV ) ;
52+ // Reset all mocks to prevent leakage
53+ vi . clearAllMocks ( ) ;
54+ } ) ;
55+
3956 describe ( 'vtfetch' , ( ) => {
4057 it ( 'parses and returns JSON, given an HttpOkResponse response' , async ( ) => {
4158 const endpoint = `/api/tablets` ;
@@ -141,6 +158,9 @@ describe('api/http', () => {
141158 } ) ;
142159
143160 it ( 'uses the fetch default `credentials` property by default' , async ( ) => {
161+ // Explicitly unset VITE_FETCH_CREDENTIALS to test default behavior
162+ delete import . meta. env . VITE_FETCH_CREDENTIALS ;
163+
144164 vi . spyOn ( global , 'fetch' ) ;
145165
146166 const endpoint = `/api/tablets` ;
@@ -157,7 +177,7 @@ describe('api/http', () => {
157177 } ) ;
158178
159179 it ( 'throws an error if an invalid value used for `credentials`' , async ( ) => {
160- ( process as any ) . env . VITE_FETCH_CREDENTIALS = 'nope' ;
180+ import . meta . env . VITE_FETCH_CREDENTIALS = 'nope' ;
161181
162182 vi . spyOn ( global , 'fetch' ) ;
163183
@@ -185,7 +205,7 @@ describe('api/http', () => {
185205 } ) ;
186206
187207 it ( 'allows GET requests when in read only mode' , async ( ) => {
188- ( process as any ) . env . VITE_READONLY_MODE = 'true' ;
208+ import . meta . env . VITE_READONLY_MODE = 'true' ;
189209
190210 const endpoint = `/api/tablets` ;
191211 const response = { ok : true , result : null } ;
@@ -199,7 +219,7 @@ describe('api/http', () => {
199219 } ) ;
200220
201221 it ( 'throws an error when executing a write request in read only mode' , async ( ) => {
202- ( process as any ) . env . VITE_READONLY_MODE = 'true' ;
222+ import . meta . env . VITE_READONLY_MODE = 'true' ;
203223
204224 vi . spyOn ( global , 'fetch' ) ;
205225
0 commit comments