test_promise_unhandled_rejection.js (4044B)
1 "use strict"; 2 3 // Tests that unhandled promise rejections generate the appropriate 4 // console messages. 5 6 const { AddonTestUtils } = ChromeUtils.importESModule( 7 "resource://testing-common/AddonTestUtils.sys.mjs" 8 ); 9 const { PromiseTestUtils } = ChromeUtils.importESModule( 10 "resource://testing-common/PromiseTestUtils.sys.mjs" 11 ); 12 13 PromiseTestUtils.expectUncaughtRejection(/could not be cloned/); 14 PromiseTestUtils.expectUncaughtRejection(/An exception was thrown/); 15 PromiseTestUtils.expectUncaughtRejection(/Bleah/); 16 17 const filename = "resource://foo/Bar.jsm"; 18 19 async function getSandboxMessages(sandbox, code) { 20 let { messages } = await AddonTestUtils.promiseConsoleOutput(async () => { 21 Cu.evalInSandbox(code, sandbox, null, filename, 1); 22 23 // We need two trips through the event loop for this error to be reported. 24 await new Promise(executeSoon); 25 await new Promise(executeSoon); 26 }); 27 28 // xpcshell tests on OS-X sometimes include an extra warning, which we 29 // unfortunately need to ignore: 30 return messages.filter( 31 msg => 32 !msg.message.includes( 33 "No chrome package registered for chrome://branding/locale/brand.properties" 34 ) 35 ); 36 } 37 38 add_task(async function test_unhandled_dom_exception() { 39 let sandbox = Cu.Sandbox(Services.scriptSecurityManager.getSystemPrincipal()); 40 sandbox.StructuredCloneHolder = StructuredCloneHolder; 41 42 let messages = await getSandboxMessages( 43 sandbox, 44 `new Promise(() => { 45 new StructuredCloneHolder("", "", () => {}); 46 });` 47 ); 48 49 equal(messages.length, 1, "Got one console message"); 50 51 let [msg] = messages; 52 ok(msg instanceof Ci.nsIScriptError, "Message is a script error"); 53 equal(msg.sourceName, filename, "Got expected filename"); 54 equal(msg.lineNumber, 2, "Got expected line number"); 55 equal( 56 msg.errorMessage, 57 "DataCloneError: Function object could not be cloned.", 58 "Got expected error message" 59 ); 60 }); 61 62 add_task(async function test_unhandled_dom_exception_wrapped() { 63 let sandbox = Cu.Sandbox( 64 Services.scriptSecurityManager.createContentPrincipalFromOrigin( 65 "http://example.com/" 66 ) 67 ); 68 Cu.exportFunction( 69 function frick() { 70 throw new Components.Exception( 71 "Bleah.", 72 Cr.NS_ERROR_FAILURE, 73 Components.stack.caller 74 ); 75 }, 76 sandbox, 77 { defineAs: "frick" } 78 ); 79 80 let messages = await getSandboxMessages( 81 sandbox, 82 `new Promise(() => { 83 frick(); 84 });` 85 ); 86 87 equal(messages.length, 2, "Got two console messages"); 88 89 let [msg1, msg2] = messages; 90 ok(msg1 instanceof Ci.nsIScriptError, "Message is a script error"); 91 equal(msg1.sourceName, filename, "Got expected filename"); 92 equal(msg1.lineNumber, 2, "Got expected line number"); 93 equal( 94 msg1.errorMessage, 95 "NS_ERROR_FAILURE: Bleah.", 96 "Got expected error message" 97 ); 98 99 ok(msg2 instanceof Ci.nsIScriptError, "Message is a script error"); 100 equal(msg2.sourceName, filename, "Got expected filename"); 101 equal(msg2.lineNumber, 2, "Got expected line number"); 102 equal( 103 msg2.errorMessage, 104 "InvalidStateError: An exception was thrown", 105 "Got expected error message" 106 ); 107 }); 108 109 add_task(async function test_unhandled_dom_exception_from_sandbox() { 110 let sandbox = Cu.Sandbox( 111 Services.scriptSecurityManager.createContentPrincipalFromOrigin( 112 "http://example.com/" 113 ), 114 { wantGlobalProperties: ["DOMException"] } 115 ); 116 let ctor = Cu.evalInSandbox("DOMException", sandbox); 117 Cu.exportFunction( 118 function frick() { 119 throw new ctor("Bleah."); 120 }, 121 sandbox, 122 { defineAs: "frick" } 123 ); 124 125 let messages = await getSandboxMessages( 126 sandbox, 127 `new Promise(() => { 128 frick(); 129 });` 130 ); 131 132 equal(messages.length, 1, "Got one console messages"); 133 134 let [msg] = messages; 135 ok(msg instanceof Ci.nsIScriptError, "Message is a script error"); 136 equal(msg.sourceName, filename, "Got expected filename"); 137 equal(msg.lineNumber, 2, "Got expected line number"); 138 equal(msg.errorMessage, "Error: Bleah.", "Got expected error message"); 139 });