tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_scripterror.html (2581B)


      1 <!DOCTYPE html>
      2 <meta charset=utf-8>
      3 <title>Tests for nsIScriptError</title>
      4 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
      5 <div id="log"></div>
      6 <script>
      7 function awaitScriptError(fun) {
      8  // Use setTimeout in order to prevent throwing from test frame
      9  // and to have a clean stack frame.
     10  setTimeout(fun, 0)
     11 
     12  return new Promise(resolve => {
     13    const observer = {
     14      QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
     15      observe(message) {
     16        if (!(message instanceof Ci.nsIScriptError)) {
     17          return;
     18        }
     19 
     20        Services.console.unregisterListener(observer);
     21        resolve(message);
     22      }
     23    };
     24 
     25    Services.console.registerListener(observer);
     26  });
     27 }
     28 
     29 function hasExpectedProperties(message, exception) {
     30  is(message.hasException, true, "has exception");
     31  is(message.exception, exception, "has correct exception");
     32 
     33  ok(message.stack != null, "has stack");
     34  is(message.stack?.source, location.href, "correct stack source")
     35 
     36  is(message.sourceName, location.href, "has correct sourceName/filename");
     37  ok(message.lineNumber > 0, "has lineNumber");
     38 
     39  is(message.innerWindowID, window.windowGlobalChild.innerWindowId,
     40     "correct innerWindowID");
     41 }
     42 
     43 add_task(async () => {
     44  await SpecialPowers.pushPrefEnv({"set": [
     45    ["javascript.options.asyncstack_capture_debuggee_only", false],
     46  ]});
     47 
     48  const TESTS = [
     49    "abc",
     50    new Error("foobar"),
     51    {foo: "bar"}
     52  ];
     53 
     54  for (let test of TESTS) {
     55    // First test as regular throw
     56    SimpleTest.expectUncaughtException();
     57    let message = await awaitScriptError(function testName() {
     58        throw test;
     59    });
     60    hasExpectedProperties(message, test);
     61    is(message.isPromiseRejection, false, "not a rejected promise");
     62 
     63    // Now test as uncaught promise rejection
     64    message = await awaitScriptError(function testName() {
     65        Promise.reject(test);
     66    });
     67    hasExpectedProperties(message, test);
     68    is(message.isPromiseRejection, true, "is a rejected promise");
     69 
     70    // Uncaught rejection from async function
     71    message = await awaitScriptError(async function testName() {
     72        throw test;
     73    });
     74    hasExpectedProperties(message, test);
     75    is(message.isPromiseRejection, true, "is a rejected promise");
     76 
     77    // Uncaught rejection from then callback
     78    message = await awaitScriptError(async function testName() {
     79        Promise.resolve().then(() => {
     80          throw test;
     81        });
     82    });
     83    hasExpectedProperties(message, test);
     84    is(message.isPromiseRejection, true, "is a rejected promise");
     85  }
     86 });
     87 </script>