|
| 1 | +/* eslint-env browser, jasmine */ |
| 2 | + |
| 3 | +window._jasmine_loadEsModule = function(src) { |
| 4 | + var script = document.createElement('script'); |
| 5 | + script.type = 'module'; |
| 6 | + |
| 7 | + // Safari reports syntax errors in ES modules as a script element error |
| 8 | + // event rather than a global error event. Rethrow so that Jasmine can |
| 9 | + // pick it up and fail the suite. |
| 10 | + script.addEventListener('error', function(event) { |
| 11 | + var msg = |
| 12 | + 'An error occurred while loading ' + |
| 13 | + src + |
| 14 | + '. Check the browser console for details.'; |
| 15 | + throw new Error(msg); |
| 16 | + }); |
| 17 | + |
| 18 | + script.src = src; |
| 19 | + document.head.appendChild(script); |
| 20 | +}; |
| 21 | + |
| 22 | +window._jasmine_loadWithTopLevelAwaitSupport = async function(scriptUrls) { |
| 23 | + const scriptsLoaded = (async function() { |
| 24 | + // Load scripts sequentially to ensure that users can get a stable order |
| 25 | + // by disabling randomization or setting a seed. This can be considerably |
| 26 | + // slower than the normal way of doing things because the browser won't |
| 27 | + // parallelize the HTTP requests. But it's the only way to ensure that the |
| 28 | + // describes will execute in a consistent order in the presence of top-level |
| 29 | + // await. |
| 30 | + for (const url of scriptUrls) { |
| 31 | + const isEsm = url.endsWith('.mjs'); |
| 32 | + |
| 33 | + if (isEsm) { |
| 34 | + try { |
| 35 | + await import(url); |
| 36 | + } catch (e) { |
| 37 | + setTimeout(function() { |
| 38 | + // Rethrow the error so jasmine-core's global error handler can pick it up. |
| 39 | + throw e; |
| 40 | + }); |
| 41 | + await new Promise(function(resolve) { |
| 42 | + setTimeout(resolve); |
| 43 | + }); |
| 44 | + } |
| 45 | + } else { |
| 46 | + await new Promise(function(resolve) { |
| 47 | + const script = document.createElement('script'); |
| 48 | + script.addEventListener('load', function() { |
| 49 | + resolve(); |
| 50 | + }); |
| 51 | + script.addEventListener('error', function() { |
| 52 | + resolve(); |
| 53 | + }); |
| 54 | + script.src = url; |
| 55 | + document.head.appendChild(script); |
| 56 | + }); |
| 57 | + } |
| 58 | + } |
| 59 | + })(); |
| 60 | + |
| 61 | + const bootJasmine = window.onload; |
| 62 | + window.onload = function() { |
| 63 | + scriptsLoaded.then(bootJasmine); |
| 64 | + }; |
| 65 | +}; |
0 commit comments