File tree 2 files changed +41
-4
lines changed
2 files changed +41
-4
lines changed Original file line number Diff line number Diff line change @@ -282,5 +282,35 @@ describe('Merkur component', () => {
282
282
done ( 'promise was rejected' ) ;
283
283
} ) ;
284
284
} ) ;
285
+
286
+ it ( 'should throw an error, if script have no source' , ( done ) => {
287
+ loadScriptAssets (
288
+ [
289
+ {
290
+ name : 'optional.js' ,
291
+ type : 'script' ,
292
+ source : { } ,
293
+ optional : true ,
294
+ } ,
295
+ {
296
+ name : 'nosource.js' ,
297
+ type : 'script' ,
298
+ source : { } ,
299
+ } ,
300
+ ] ,
301
+ rootElement
302
+ )
303
+ . then ( ( ) => {
304
+ done ( 'did not reject' ) ;
305
+ } )
306
+ . catch ( ( error ) => {
307
+ expect ( error . message ) . toBe (
308
+ "Asset 'nosource.js' is missing ES variant and could not be loaded."
309
+ ) ;
310
+ expect ( error . asset ) . toBeTruthy ( ) ;
311
+
312
+ done ( ) ;
313
+ } ) ;
314
+ } ) ;
285
315
} ) ;
286
316
} ) ;
Original file line number Diff line number Diff line change @@ -90,7 +90,7 @@ function loadStyleAssets(assets, root = document.head) {
90
90
return Promise . all ( stylesToRender . map ( ( asset ) => _loadStyle ( asset , root ) ) ) ;
91
91
}
92
92
93
- function loadScriptAssets ( assets , root = document . head ) {
93
+ async function loadScriptAssets ( assets , root = document . head ) {
94
94
const scriptElements = root . querySelectorAll ( 'script' ) ;
95
95
const scriptsToRender = assets . reduce ( ( scripts , asset ) => {
96
96
const { source } = asset ;
@@ -110,9 +110,16 @@ function loadScriptAssets(assets, root = document.head) {
110
110
}
111
111
112
112
if ( ! _asset . source ) {
113
- console . warn (
114
- `Asset '${ _asset . name } ' is missing ES variant and could not be loaded.`
115
- ) ;
113
+ const message = `Asset '${ _asset . name } ' is missing ES variant and could not be loaded.` ;
114
+
115
+ if ( ! _asset . optional ) {
116
+ const error = new Error ( message ) ;
117
+ error . asset = _asset ;
118
+
119
+ throw error ;
120
+ }
121
+
122
+ console . warn ( message ) ;
116
123
return scripts ;
117
124
}
118
125
}
You can’t perform that action at this time.
0 commit comments