browser_jsterm_await_error.js (5866B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Test that failing top-level await expression (rejected or throwing) work as expected. 5 6 "use strict"; 7 8 const TEST_URI = 9 "data:text/html;charset=utf-8,<!DOCTYPE html>Web Console test failing top-level await"; 10 11 add_task(async function () { 12 // Needed for the execute() function below 13 await pushPref("security.allow_parent_unrestricted_js_loads", true); 14 15 // Enable await mapping. 16 await pushPref("devtools.debugger.features.map-await-expression", true); 17 const hud = await openNewTabAndConsole(TEST_URI); 18 19 info("Check that awaiting for a rejecting promise displays an error"); 20 let res = await executeAndWaitForErrorMessage( 21 hud, 22 `await new Promise((resolve,reject) => setTimeout(() => reject("await-rej"), 250))`, 23 "Uncaught (in promise) await-rej" 24 ); 25 ok(res.node, "awaiting for a rejecting promise displays an error message"); 26 27 res = await executeAndWaitForErrorMessage( 28 hud, 29 `await Promise.reject("await-rej-2")`, 30 `Uncaught (in promise) await-rej-2` 31 ); 32 ok(res.node, "awaiting for Promise.reject displays an error"); 33 34 res = await executeAndWaitForErrorMessage( 35 hud, 36 `await Promise.reject("")`, 37 `Uncaught (in promise) <empty string>` 38 ); 39 ok( 40 res.node, 41 "awaiting for Promise rejecting with empty string displays the expected error" 42 ); 43 44 res = await executeAndWaitForErrorMessage( 45 hud, 46 `await Promise.reject(null)`, 47 `Uncaught (in promise) null` 48 ); 49 ok( 50 res.node, 51 "awaiting for Promise rejecting with null displays the expected error" 52 ); 53 54 res = await executeAndWaitForErrorMessage( 55 hud, 56 `await Promise.reject(undefined)`, 57 `Uncaught (in promise) undefined` 58 ); 59 ok( 60 res.node, 61 "awaiting for Promise rejecting with undefined displays the expected error" 62 ); 63 64 res = await executeAndWaitForErrorMessage( 65 hud, 66 `await Promise.reject(false)`, 67 `Uncaught (in promise) false` 68 ); 69 ok( 70 res.node, 71 "awaiting for Promise rejecting with false displays the expected error" 72 ); 73 74 res = await executeAndWaitForErrorMessage( 75 hud, 76 `await Promise.reject(0)`, 77 `Uncaught (in promise) 0` 78 ); 79 ok( 80 res.node, 81 "awaiting for Promise rejecting with 0 displays the expected error" 82 ); 83 84 res = await executeAndWaitForErrorMessage( 85 hud, 86 `await Promise.reject({foo: "bar"})`, 87 `Uncaught (in promise) Object { foo: "bar" }` 88 ); 89 ok( 90 res.node, 91 "awaiting for Promise rejecting with an object displays the expected error" 92 ); 93 94 res = await executeAndWaitForErrorMessage( 95 hud, 96 `await Promise.reject(new Error("foo"))`, 97 `Uncaught (in promise) Error: foo` 98 ); 99 ok( 100 res.node, 101 "awaiting for Promise rejecting with an error object displays the expected error" 102 ); 103 104 res = await executeAndWaitForErrorMessage( 105 hud, 106 `var err = new Error("foo"); 107 err.name = "CustomError"; 108 await Promise.reject(err); 109 `, 110 `Uncaught (in promise) CustomError: foo` 111 ); 112 ok( 113 res.node, 114 "awaiting for Promise rejecting with an error object with a name property displays the expected error" 115 ); 116 117 res = await executeAndWaitForErrorMessage( 118 hud, 119 `await new Promise(() => a.b.c)`, 120 `ReferenceError: a is not defined` 121 ); 122 ok( 123 res.node, 124 "awaiting for a promise with a throwing function displays an error" 125 ); 126 127 res = await executeAndWaitForErrorMessage( 128 hud, 129 `await new Promise(res => setTimeout(() => res(d.e.f), 250))`, 130 `ReferenceError: d is not defined` 131 ); 132 ok( 133 res.node, 134 "awaiting for a promise with a throwing function displays an error" 135 ); 136 137 res = await executeAndWaitForErrorMessage( 138 hud, 139 `await new Promise(res => { throw "instant throw"; })`, 140 `Uncaught (in promise) instant throw` 141 ); 142 ok( 143 res.node, 144 "awaiting for a promise with a throwing function displays an error" 145 ); 146 147 res = await executeAndWaitForErrorMessage( 148 hud, 149 `await new Promise(res => { throw new Error("instant error throw"); })`, 150 `Error: instant error throw` 151 ); 152 ok( 153 res.node, 154 "awaiting for a promise with a thrown Error displays an error message" 155 ); 156 157 res = await executeAndWaitForErrorMessage( 158 hud, 159 `await new Promise(res => { setTimeout(() => { throw "throw in timeout"; }, 250) })`, 160 `Uncaught throw in timeout` 161 ); 162 ok( 163 res.node, 164 "awaiting for a promise with a throwing function displays an error" 165 ); 166 167 res = await executeAndWaitForErrorMessage( 168 hud, 169 `await new Promise(res => { 170 setTimeout(() => { throw new Error("throw error in timeout"); }, 250) 171 })`, 172 `throw error in timeout` 173 ); 174 ok( 175 res.node, 176 "awaiting for a promise with a throwing function displays an error" 177 ); 178 179 info("Check that we have the expected number of commands"); 180 const expectedInputsNumber = 16; 181 is( 182 (await findMessagesVirtualizedByType({ hud, typeSelector: ".command" })) 183 .length, 184 expectedInputsNumber, 185 "There is the expected number of commands messages" 186 ); 187 188 info("Check that we have as many errors as commands"); 189 const expectedErrorsNumber = expectedInputsNumber; 190 is( 191 (await findMessagesVirtualizedByType({ hud, typeSelector: ".error" })) 192 .length, 193 expectedErrorsNumber, 194 "There is the expected number of error messages" 195 ); 196 197 info("Check that there's no result message"); 198 is( 199 (await findMessagesVirtualizedByType({ hud, typeSelector: ".result" })) 200 .length, 201 0, 202 "There is no result messages" 203 ); 204 205 info("Check that malformed await expressions displays a meaningful error"); 206 res = await executeAndWaitForErrorMessage( 207 hud, 208 `await new Promise())`, 209 `SyntaxError: unexpected token: ')'` 210 ); 211 ok( 212 res.node, 213 "awaiting for a malformed expression displays a meaningful error" 214 ); 215 });