Skip to content

Commit 96ee05b

Browse files
committed
Add another ShadowRealm test for onerror
This test is similar to window-onerror-runtime-error-throw.html and window-onerror-runtime-error.html, but ensuring that the ShadowRealm global's onerror is triggered and not the Window's onerror. By my reading of the spec, we cannot have a test equivalent to window-onerror-parse-error.html in ShadowRealm because ShadowRealm.prototype.evaluate() will exit early if the given code fails to parse.
1 parent 692092b commit 96ee05b

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// META: title=globalThis.onerror: runtime script errors in ShadowRealm
2+
3+
// https://html.spec.whatwg.org/multipage/#runtime-script-errors says what to do
4+
// for uncaught runtime script errors, and just below describes what to do when
5+
// onerror is a Function.
6+
7+
async_test(t => {
8+
onerror = t.unreached_func("Window onerror should not be triggered");
9+
10+
const realm = new ShadowRealm();
11+
12+
realm.evaluate("var errorCount = 0;");
13+
realm.evaluate(`(doAsserts) => {
14+
globalThis.onerror = function(msg, url, lineno, colno, thrownValue) {
15+
++errorCount;
16+
doAsserts(url, lineno, colno, typeof thrownValue, String(thrownValue));
17+
};
18+
}`)(t.step_func((url, lineno, typeofThrownValue, stringifiedThrownValue) => {
19+
assert_equals(url, "eval code", "correct url passed to onerror");
20+
assert_equals(lineno, 8, "correct line number passed to onerror");
21+
assert_equals(typeofThrownValue, "string", "thrown string passed directly to onerror");
22+
assert_equals(stringifiedThrownValue, "bar", "correct thrown value passed to onerror");
23+
}));
24+
25+
assert_throws_js(TypeError, () => realm.evaluate(`
26+
try {
27+
// This error is caught, so it should NOT trigger onerror.
28+
throw "foo";
29+
} catch (ex) {
30+
}
31+
// This error is NOT caught, so it should trigger onerror.
32+
throw "bar";
33+
`), "thrown error is wrapped in a TypeError object from the surrounding realm");
34+
35+
t.step_timeout(() => {
36+
assert_equals(realm.evaluate("errorCount"), 1, "onerror should be called once");
37+
}, 1000);
38+
}, "onerror triggered by uncaught thrown exception in realm.evaluate");
39+
40+
async_test(t => {
41+
onerror = t.unreached_func("Window onerror should not be triggered");
42+
43+
const realm = new ShadowRealm();
44+
45+
realm.evaluate("var errorCount = 0;");
46+
realm.evaluate(`(doAsserts) => {
47+
globalThis.onerror = function(msg, url, lineno, colno, thrownValue) {
48+
++errorCount;
49+
doAsserts(url, lineno, typeof thrownValue, thrownValue instanceof TypeError);
50+
};
51+
}`)(t.step_func((url, lineno, typeofThrownValue, isTypeError) => {
52+
assert_equals(url, "eval code", "correct url passed to onerror");
53+
assert_equals(lineno, 8, "correct line number passed to onerror");
54+
assert_equals(typeofThrownValue, "object", "thrown error instance passed to onerror");
55+
assert_true(isShadowRealmTypeError, "correct thrown value passed to onerror");
56+
}));
57+
58+
assert_throws_js(TypeError, () => realm.evaluate(`
59+
try {
60+
// This error is caught, so it should NOT trigger onerror.
61+
window.nonexistentproperty.oops();
62+
} catch (ex) {
63+
}
64+
// This error is NOT caught, so it should trigger onerror.
65+
window.nonexistentproperty.oops();
66+
`), "thrown error is wrapped in a TypeError object from the surrounding realm");
67+
68+
t.step_timeout(() => {
69+
assert_equals(realm.evaluate("errorCount"), 1, "onerror should be called once");
70+
}, 1000);
71+
}, "onerror triggered by uncaught runtime error in realm.evaluate");

0 commit comments

Comments
 (0)