browser_dbg-es-module.js (3345B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 // Test that we can debug ES Modules. 6 7 "use strict"; 8 9 const httpServer = createTestHTTPServer(); 10 httpServer.registerContentType("html", "text/html"); 11 12 httpServer.registerPathHandler(`/index.html`, function (request, response) { 13 response.setStatusLine(request.httpVersion, 200, "OK"); 14 response.write( 15 `<html> 16 <script type="module"> 17 import * as mod from "./module.mjs"; 18 window.inline = () => { 19 console.log("inline"); 20 }; 21 window.onload = async () => { 22 // Force the module to be loaded a bit late 23 await new Promise(r => setTimeout(r, 1000)); 24 25 import("./late-module.mjs"); 26 } 27 </script> 28 </html>` 29 ); 30 }); 31 32 httpServer.registerPathHandler("/module.mjs", function (request, response) { 33 response.setHeader("Content-Type", "application/javascript"); 34 response.write( 35 `export const bar = 24; 36 window.regular = () => { 37 console.log("regular"); 38 }; 39 ` 40 ); 41 }); 42 43 httpServer.registerPathHandler( 44 "/late-module.mjs", 45 function (request, response) { 46 response.setHeader("Content-Type", "application/javascript"); 47 response.write( 48 `export const late = "foo"; 49 window.late = () => { 50 console.log("late"); 51 }; 52 console.log("late module loaded"); 53 ` 54 ); 55 } 56 ); 57 const port = httpServer.identity.primaryPort; 58 const TEST_URL = `http://localhost:${port}/`; 59 60 add_task(async function () { 61 const dbg = await initDebuggerWithAbsoluteURL( 62 TEST_URL + "index.html", 63 "index.html", 64 "module.mjs", 65 "late-module.mjs" 66 ); 67 68 { 69 info("Pausing in inlined ESM"); 70 const source = findSource(dbg, "index.html"); 71 await selectSource(dbg, source); 72 await addBreakpoint(dbg, source, 5); 73 74 const onResumed = SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => { 75 content.wrappedJSObject.inline(); 76 }); 77 78 await waitForPaused(dbg); 79 await assertPausedAtSourceAndLine(dbg, source.id, 5); 80 assertTextContentOnLine(dbg, 5, `console.log("inline");`); 81 82 await resume(dbg); 83 await onResumed; 84 } 85 86 { 87 info("Pausing in regular ESM"); 88 const source = findSource(dbg, "module.mjs"); 89 await selectSource(dbg, source); 90 await addBreakpoint(dbg, source, 3); 91 92 const onResumed = SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => { 93 content.wrappedJSObject.regular(); 94 }); 95 96 await waitForPaused(dbg); 97 await assertPausedAtSourceAndLine(dbg, source.id, 3); 98 assertTextContentOnLine(dbg, 3, `console.log("regular");`); 99 100 await resume(dbg); 101 await onResumed; 102 } 103 104 { 105 info("Pausing in late ESM (loaded lazily via async import()"); 106 const source = findSource(dbg, "late-module.mjs"); 107 await selectSource(dbg, source); 108 await addBreakpoint(dbg, source, 3); 109 110 const onResumed = SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => { 111 content.wrappedJSObject.late(); 112 }); 113 114 await waitForPaused(dbg); 115 await assertPausedAtSourceAndLine(dbg, source.id, 3); 116 assertTextContentOnLine(dbg, 3, `console.log("late");`); 117 118 await resume(dbg); 119 await onResumed; 120 } 121 });