@@ -160,6 +160,77 @@ describe('frontend-webpack-project example', () => {
160
160
} ) ;
161
161
} ) ;
162
162
163
+ it ( 'fills in currentEnvironment function' , async ( ) => {
164
+ process . env . APP_CONFIG = JSON . stringify ( { externalApiUrl : 'https://localhost:3999' } ) ;
165
+
166
+ await new Promise < void > ( ( done , reject ) => {
167
+ webpack ( [ createOptions ( { } , true ) ] , ( err , stats ) => {
168
+ if ( err ) return reject ( err ) ;
169
+ if ( ! stats ) return reject ( new Error ( 'no stats' ) ) ;
170
+ if ( stats . hasErrors ( ) ) reject ( stats . toString ( ) ) ;
171
+
172
+ const { children } = stats . toJson ( { source : true } ) ;
173
+ const [ { modules = [ ] } ] = children || [ ] ;
174
+
175
+ expect (
176
+ modules . some ( ( { source } ) => source ?. includes ( 'export function currentEnvironment()' ) ) ,
177
+ ) . toBe ( true ) ;
178
+ expect ( modules . some ( ( { source } ) => source ?. includes ( 'return "test";' ) ) ) . toBe ( true ) ;
179
+
180
+ done ( ) ;
181
+ } ) ;
182
+ } ) ;
183
+ } ) ;
184
+
185
+ it ( 'fills in currentEnvironment function with custom environment' , async ( ) => {
186
+ process . env . APP_CONFIG = JSON . stringify ( { externalApiUrl : 'https://localhost:3999' } ) ;
187
+ process . env . APP_CONFIG_ENV = 'foobar' ;
188
+
189
+ await new Promise < void > ( ( done , reject ) => {
190
+ webpack ( [ createOptions ( { } , true ) ] , ( err , stats ) => {
191
+ if ( err ) return reject ( err ) ;
192
+ if ( ! stats ) return reject ( new Error ( 'no stats' ) ) ;
193
+ if ( stats . hasErrors ( ) ) reject ( stats . toString ( ) ) ;
194
+
195
+ const { children } = stats . toJson ( { source : true } ) ;
196
+ const [ { modules = [ ] } ] = children || [ ] ;
197
+
198
+ expect (
199
+ modules . some ( ( { source } ) => source ?. includes ( 'export function currentEnvironment()' ) ) ,
200
+ ) . toBe ( true ) ;
201
+ expect ( modules . some ( ( { source } ) => source ?. includes ( 'return "foobar";' ) ) ) . toBe ( true ) ;
202
+
203
+ done ( ) ;
204
+ } ) ;
205
+ } ) ;
206
+ } ) ;
207
+
208
+ it ( 'uses custom options for currentEnvironment' , async ( ) => {
209
+ process . env . APP_CONFIG = JSON . stringify ( { externalApiUrl : 'https://localhost:3999' } ) ;
210
+ process . env . APP_CONFIG_ENV = 'test' ;
211
+
212
+ await new Promise < void > ( ( done , reject ) => {
213
+ webpack (
214
+ [ createOptions ( { loading : { environmentOverride : 'foobar' } } , true ) ] ,
215
+ ( err , stats ) => {
216
+ if ( err ) return reject ( err ) ;
217
+ if ( ! stats ) return reject ( new Error ( 'no stats' ) ) ;
218
+ if ( stats . hasErrors ( ) ) reject ( stats . toString ( ) ) ;
219
+
220
+ const { children } = stats . toJson ( { source : true } ) ;
221
+ const [ { modules = [ ] } ] = children || [ ] ;
222
+
223
+ expect (
224
+ modules . some ( ( { source } ) => source ?. includes ( 'export function currentEnvironment()' ) ) ,
225
+ ) . toBe ( true ) ;
226
+ expect ( modules . some ( ( { source } ) => source ?. includes ( 'return "foobar";' ) ) ) . toBe ( true ) ;
227
+
228
+ done ( ) ;
229
+ } ,
230
+ ) ;
231
+ } ) ;
232
+ } ) ;
233
+
163
234
it . skip ( 'does not bundle the validateConfig function' , async ( ) => {
164
235
process . env . APP_CONFIG = JSON . stringify ( { externalApiUrl : 'https://localhost:3999' } ) ;
165
236
0 commit comments