<!DOCTYPEhtml>
<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;
}
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>
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.