Bug 1374241 - Add self-tests for unhandled rejection handling in browser-chrome tests. r=Mossop
authorPaolo Amadini <paolo.mozmail@amadzone.org>
Thu, 29 Jun 2017 00:12:58 +0100
changeset 601752 8b0b1a47321a212e29bd9cfecbe80e0bfa04fe25
parent 601751 e54513aa792225bdef5d7c0165f1d86ed189bcef
child 601753 37651b6bd99ceaeffa17b2a2e02f6e846fa3a3cf
push id66200
push userhchang@mozilla.com
push dateThu, 29 Jun 2017 03:53:43 +0000
reviewersMossop
bugs1374241
milestone56.0a1
Bug 1374241 - Add self-tests for unhandled rejection handling in browser-chrome tests. r=Mossop MozReview-Commit-ID: 9gloXfhfZ3x
testing/mochitest/tests/browser/browser.ini
testing/mochitest/tests/browser/browser_fail_add_task_uncaught_rejection.js
testing/mochitest/tests/browser/browser_fail_uncaught_rejection.js
testing/mochitest/tests/browser/browser_fail_uncaught_rejection_expected.js
testing/mochitest/tests/browser/browser_fail_uncaught_rejection_expected_multi.js
testing/mochitest/tests/browser/browser_uncaught_rejection_expected.js
--- a/testing/mochitest/tests/browser/browser.ini
+++ b/testing/mochitest/tests/browser/browser.ini
@@ -5,35 +5,39 @@ support-files =
 [browser_add_task.js]
 [browser_async.js]
 [browser_browserLoaded_content_loaded.js]
 [browser_BrowserTestUtils.js]
 support-files =
   dummy.html
 [browser_fail.js]
 [browser_fail_add_task.js]
+[browser_fail_add_task_uncaught_rejection.js]
 [browser_fail_async.js]
 [browser_fail_if.js]
 fail-if = true
 [browser_fail_throw.js]
 [browser_fail_timeout.js]
 skip-if = true # Disabled beacuse it takes too long (bug 1178959)
 [browser_fail_uncaught_rejection.js]
+[browser_fail_uncaught_rejection_expected.js]
+[browser_fail_uncaught_rejection_expected_multi.js]
 [browser_fail_unexpectedTimeout.js]
 skip-if = true # Disabled beacuse it takes too long (bug 1178959)
 [browser_getTestFile.js]
 support-files =
   test-dir/*
   waitForFocusPage.html
 [browser_head.js]
 [browser_pass.js]
 [browser_parameters.js]
 [browser_popupNode.js]
 [browser_popupNode_check.js]
 [browser_privileges.js]
 [browser_requestLongerTimeout.js]
 skip-if = true # Disabled beacuse it takes too long (bug 1178959)
 [browser_sanityException.js]
 [browser_sanityException2.js]
+[browser_uncaught_rejection_expected.js]
 [browser_waitForFocus.js]
 skip-if = (os == "win" && e10s && debug)
 [browser_zz_fail_openwindow.js]
 skip-if = true # this catches outside of the main loop to find an extra window
copy from testing/mochitest/tests/browser/browser_fail_add_task.js
copy to testing/mochitest/tests/browser/browser_fail_add_task_uncaught_rejection.js
--- a/testing/mochitest/tests/browser/browser_fail_add_task.js
+++ b/testing/mochitest/tests/browser/browser_fail_add_task_uncaught_rejection.js
@@ -1,17 +1,23 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
-setExpectedFailuresForSelfTest(4);
+setExpectedFailuresForSelfTest(8);
+
+// Keep "JSMPromise" separate so "Promise" still refers to native Promises.
+let JSMPromise = Cu.import("resource://gre/modules/Promise.jsm", {}).Promise;
 
-function rejectOnNextTick(error) {
-  return new Promise((resolve, reject) => executeSoon(() => reject(error)));
+async function rejectOnNextTick(error) {
+  await Promise.resolve();
+
+  Promise.reject(error);
+  JSMPromise.reject(error);
 }
 
 add_task(async function failWithoutError() {
   await rejectOnNextTick(undefined);
 });
 
 add_task(async function failWithString() {
   await rejectOnNextTick("This is a string");
--- a/testing/mochitest/tests/browser/browser_fail_uncaught_rejection.js
+++ b/testing/mochitest/tests/browser/browser_fail_uncaught_rejection.js
@@ -1,6 +1,19 @@
-setExpectedFailuresForSelfTest(1);
+setExpectedFailuresForSelfTest(3);
+
+// Keep "JSMPromise" separate so "Promise" still refers to native Promises.
+let JSMPromise = Cu.import("resource://gre/modules/Promise.jsm", {}).Promise;
 
 function test() {
-  Components.utils.import("resource://gre/modules/Promise.jsm", this);
   Promise.reject(new Error("Promise rejection."));
+  JSMPromise.reject(new Error("Promise.jsm rejection."));
+  (async () => {
+    throw "Synchronous rejection from async function.";
+  })();
+
+  // The following rejections are caught, so they won't result in failures.
+  Promise.reject(new Error("Promise rejection.")).catch(() => {});
+  JSMPromise.reject(new Error("Promise.jsm rejection.")).catch(() => {});
+  (async () => {
+    throw "Synchronous rejection from async function.";
+  })().catch(() => {});
 }
new file mode 100644
--- /dev/null
+++ b/testing/mochitest/tests/browser/browser_fail_uncaught_rejection_expected.js
@@ -0,0 +1,10 @@
+setExpectedFailuresForSelfTest(1);
+
+// The test will fail because there is only one of two expected rejections.
+Cu.import("resource://testing-common/PromiseTestUtils.jsm", this);
+PromiseTestUtils.expectUncaughtRejection(/Promise rejection./);
+PromiseTestUtils.expectUncaughtRejection(/Promise rejection./);
+
+function test() {
+  Promise.reject(new Error("Promise rejection."));
+}
new file mode 100644
--- /dev/null
+++ b/testing/mochitest/tests/browser/browser_fail_uncaught_rejection_expected_multi.js
@@ -0,0 +1,9 @@
+setExpectedFailuresForSelfTest(1);
+
+// The test will fail because an expected uncaught rejection is actually caught.
+Cu.import("resource://testing-common/PromiseTestUtils.jsm", this);
+PromiseTestUtils.expectUncaughtRejection(/Promise rejection./);
+
+function test() {
+  Promise.reject(new Error("Promise rejection.")).catch(() => {});
+}
new file mode 100644
--- /dev/null
+++ b/testing/mochitest/tests/browser/browser_uncaught_rejection_expected.js
@@ -0,0 +1,18 @@
+// Keep "JSMPromise" separate so "Promise" still refers to native Promises.
+let JSMPromise = Cu.import("resource://gre/modules/Promise.jsm", {}).Promise;
+
+Cu.import("resource://testing-common/PromiseTestUtils.jsm", this);
+PromiseTestUtils.whitelistRejectionsGlobally(/Whitelisted rejection./);
+PromiseTestUtils.expectUncaughtRejection(/Promise.jsm rejection./);
+PromiseTestUtils.expectUncaughtRejection(/Promise.jsm rejection./);
+PromiseTestUtils.expectUncaughtRejection(/Promise rejection./);
+PromiseTestUtils.expectUncaughtRejection(/Promise rejection./);
+
+function test() {
+  Promise.reject(new Error("Promise rejection."));
+  Promise.reject(new Error("Promise rejection."));
+  Promise.reject(new Error("Whitelisted rejection."));
+  JSMPromise.reject(new Error("Promise.jsm rejection."));
+  JSMPromise.reject(new Error("Promise.jsm rejection."));
+  JSMPromise.reject(new Error("Whitelisted rejection."));
+}