1
+ import { vi , describe , it , expect , beforeEach , afterAll } from 'vitest' ;
1
2
import { render , screen , waitFor } from '@testing-library/react' ;
3
+
4
+ // Create a mutable version of ImportMetaEnv for testing
5
+ type MutableImportMetaEnv = {
6
+ - readonly [ K in keyof ImportMetaEnv ] : ImportMetaEnv [ K ] ;
7
+ } ;
8
+
9
+ // Mock the module before importing
10
+ vi . mock ( '../ConfigContext' , async ( ) => {
11
+
12
+ // Create base mock environment
13
+ const mockEnv : MutableImportMetaEnv = {
14
+ MODE : 'test' ,
15
+ DEV : true ,
16
+ PROD : false ,
17
+ BASE_URL : '/' ,
18
+ VITE_CONFIG_GIST_URL : undefined
19
+ } ;
20
+
21
+ // Create a proxy to handle property assignments
22
+ const envProxy = new Proxy < MutableImportMetaEnv > ( mockEnv , {
23
+ set ( target : MutableImportMetaEnv , prop : string , value : unknown ) {
24
+ target [ prop ] = value ;
25
+ return true ;
26
+ }
27
+ } ) ;
28
+
29
+ // Mock import.meta
30
+ vi . stubGlobal ( 'import' , {
31
+ meta : {
32
+ env : envProxy
33
+ }
34
+ } ) ;
35
+
36
+ const actual = await vi . importActual < typeof import ( '../ConfigContext' ) > ( '../ConfigContext' ) ;
37
+ return actual ;
38
+ } ) ;
39
+
40
+ // Now import the components that use import.meta.env
2
41
import { ConfigProvider , useConfig } from '../ConfigContext' ;
3
- import { vi , describe , it , expect , beforeEach } from 'vitest' ;
4
42
5
43
// Mock component to test the useConfig hook
6
44
const TestComponent = ( ) => {
@@ -17,27 +55,24 @@ const TestComponent = () => {
17
55
18
56
describe ( 'ConfigContext' , ( ) => {
19
57
const mockGistUrl = 'https://api.github.com/gists/123' ;
58
+ beforeAll ( ( ) => {
59
+ // Mock console methods to suppress expected logs
60
+ vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { /* no-op */ } ) ;
61
+ vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { /* no-op */ } ) ;
62
+ } ) ;
63
+
20
64
beforeEach ( ( ) => {
21
- // Reset mocks and spies
22
- vi . restoreAllMocks ( ) ;
65
+ // Reset mocks before each test
23
66
vi . resetModules ( ) ;
67
+ vi . clearAllMocks ( ) ;
24
68
25
- // Mock fetch to return error by default
69
+ // Reset fetch mock
26
70
global . fetch = vi . fn ( ) . mockRejectedValue ( new Error ( 'Fetch not mocked' ) ) ;
27
-
28
- // Mock Vite's import.meta.env
29
- vi . stubGlobal ( 'import.meta' , {
30
- env : {
31
- VITE_CONFIG_GIST_URL : undefined ,
32
- MODE : 'test' ,
33
- DEV : true
34
- }
35
- } ) ;
36
71
} ) ;
37
72
38
- afterEach ( ( ) => {
73
+ afterAll ( ( ) => {
39
74
vi . unstubAllGlobals ( ) ;
40
- vi . clearAllMocks ( ) ;
75
+ vi . restoreAllMocks ( ) ; // Restore console methods
41
76
} ) ;
42
77
43
78
it ( 'loads default config when no gist URL is provided' , async ( ) => {
@@ -54,12 +89,8 @@ describe('ConfigContext', () => {
54
89
} ) ;
55
90
56
91
it ( 'loads and transforms gist config successfully' , async ( ) => {
57
- // Update env mock with gist URL
58
- vi . stubGlobal ( 'import.meta' , {
59
- env : {
60
- VITE_CONFIG_GIST_URL : mockGistUrl
61
- }
62
- } ) ;
92
+ // Update env through the proxy
93
+ ( import . meta. env as MutableImportMetaEnv ) . VITE_CONFIG_GIST_URL = mockGistUrl ;
63
94
64
95
// Mock successful gist response
65
96
const mockGistData = {
@@ -104,12 +135,8 @@ describe('ConfigContext', () => {
104
135
} ) ;
105
136
106
137
it ( 'falls back to default config on fetch error' , async ( ) => {
107
- // Update env mock with gist URL
108
- vi . stubGlobal ( 'import.meta' , {
109
- env : {
110
- VITE_CONFIG_GIST_URL : mockGistUrl
111
- }
112
- } ) ;
138
+ // Update env through the proxy
139
+ ( import . meta. env as MutableImportMetaEnv ) . VITE_CONFIG_GIST_URL = mockGistUrl ;
113
140
114
141
global . fetch = vi . fn ( ) . mockImplementation ( ( ) =>
115
142
Promise . reject ( new Error ( 'Network error' ) )
@@ -128,12 +155,8 @@ describe('ConfigContext', () => {
128
155
} ) ;
129
156
130
157
it ( 'handles malformed gist data gracefully' , async ( ) => {
131
- // Update env mock with gist URL
132
- vi . stubGlobal ( 'import.meta' , {
133
- env : {
134
- VITE_CONFIG_GIST_URL : mockGistUrl
135
- }
136
- } ) ;
158
+ // Update env through the proxy
159
+ ( import . meta. env as MutableImportMetaEnv ) . VITE_CONFIG_GIST_URL = mockGistUrl ;
137
160
138
161
global . fetch = vi . fn ( ) . mockImplementation ( ( ) =>
139
162
Promise . resolve ( {
@@ -155,12 +178,8 @@ describe('ConfigContext', () => {
155
178
} ) ;
156
179
157
180
it ( 'transforms partial gist data correctly' , async ( ) => {
158
- // Update env mock with gist URL
159
- vi . stubGlobal ( 'import.meta' , {
160
- env : {
161
- VITE_CONFIG_GIST_URL : mockGistUrl
162
- }
163
- } ) ;
181
+ // Update env through the proxy
182
+ ( import . meta. env as MutableImportMetaEnv ) . VITE_CONFIG_GIST_URL = mockGistUrl ;
164
183
165
184
// Mock partial data with missing fields
166
185
const mockPartialData = {
0 commit comments