summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/chrome/test_scripterror.html
blob: 38cb3164677b01698a1697a0d8777ccbbebb28b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<meta charset=utf-8>
<title>Tests for nsIScriptError</title>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<div id="log"></div>
<script>
function awaitScriptError(fun) {
  // Use setTimeout in order to prevent throwing from test frame
  // and to have a clean stack frame.
  setTimeout(fun, 0)

  return new Promise(resolve => {
    const observer = {
      QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
      observe(message) {
        if (!(message instanceof Ci.nsIScriptError)) {
          return;
        }

        Services.console.unregisterListener(observer);
        resolve(message);
      }
    };

    Services.console.registerListener(observer);
  });
}

function hasExpectedProperties(message, exception) {
  is(message.hasException, true, "has exception");
  is(message.exception, exception, "has correct exception");

  ok(message.stack != null, "has stack");
  is(message.stack?.source, location.href, "correct stack source")

  is(message.sourceName, location.href, "has correct sourceName/filename");
  ok(message.lineNumber > 0, "has lineNumber");

  is(message.innerWindowID, window.windowGlobalChild.innerWindowId,
     "correct innerWindowID");
}

add_task(async () => {
  await SpecialPowers.pushPrefEnv({"set": [
    ["javascript.options.asyncstack_capture_debuggee_only", false],
  ]});

  const TESTS = [
    "abc",
    new Error("foobar"),
    {foo: "bar"}
  ];

  for (let test of TESTS) {
    // First test as regular throw
    SimpleTest.expectUncaughtException();
    let message = await awaitScriptError(function testName() {
        throw test;
    });
    hasExpectedProperties(message, test);
    is(message.isPromiseRejection, false, "not a rejected promise");

    // Now test as uncaught promise rejection
    message = await awaitScriptError(function testName() {
        Promise.reject(test);
    });
    hasExpectedProperties(message, test);
    is(message.isPromiseRejection, true, "is a rejected promise");

    // Uncaught rejection from async function
    message = await awaitScriptError(async function testName() {
        throw test;
    });
    hasExpectedProperties(message, test);
    is(message.isPromiseRejection, true, "is a rejected promise");

    // Uncaught rejection from then callback
    message = await awaitScriptError(async function testName() {
        Promise.resolve().then(() => {
          throw test;
        });
    });
    hasExpectedProperties(message, test);
    is(message.isPromiseRejection, true, "is a rejected promise");
  }
});
</script>