Bug 1426219 - Assert.throws now cleans up recentJSDevError;r?mdeboer draft
authorDavid Teller <dteller@mozilla.com>
Tue, 19 Dec 2017 22:08:23 +0100
changeset 713223 f66a3ca0cdecc229aa25f540ff776b2c115f283b
parent 713222 b204100c3f927e937db0fbdd2f837f39ae957188
child 744279 2715edc1ccf99951d9909f4b54713d3ef973b62f
push id93582
push userdteller@mozilla.com
push dateTue, 19 Dec 2017 21:18:14 +0000
reviewersmdeboer
bugs1426219
milestone59.0a1
Bug 1426219 - Assert.throws now cleans up recentJSDevError;r?mdeboer Assert.throws is used when we expect an error. This error may be stored in recentJSDevError and cause test failures, so we need to clean it up in case of success. MozReview-Commit-ID: 2tB4iyAtGFd
testing/modules/Assert.jsm
--- a/testing/modules/Assert.jsm
+++ b/testing/modules/Assert.jsm
@@ -356,16 +356,30 @@ function expectedException(actual, expec
  *        (mixed) This parameter can be either a RegExp, a function, or a string. The
  *        function is either the error type's constructor, or it's a method that returns a boolean
  *        that describes the test outcome. When string value is provided, it will be used as if it
  *        was provided as the message parameter.
  * @param message (optional)
  *        (string) Short explanation of the expected result
  */
 proto.throws = function(block, expected, message) {
+  // `true` if we realize that we have added an
+  // error to `ChromeUtils.recentJSDevError` and
+  // that we probably need to clean it up.
+  let cleanupRecentJSDevError = false;
+  if ("recentJSDevError" in ChromeUtils) {
+    // Check that we're in a build of Firefox that supports
+    // the `recentJSDevError` mechanism (i.e. Nightly build).
+    if (ChromeUtils.recentJSDevError == undefined) {
+      // There was no previous error, so if we throw
+      // an error here, we may need to clean it up.
+      cleanupRecentJSDevError = true;
+    }
+  }
+
   let actual;
 
   if (typeof expected === "string") {
     message = expected;
     expected = null;
   }
 
   try {
@@ -381,16 +395,28 @@ proto.throws = function(block, expected,
     this.report(true, actual, expected, "Missing expected exception" + message);
   }
 
   if ((actual && expected && !expectedException(actual, expected))) {
     throw actual;
   }
 
   this.report(false, expected, expected, message);
+
+  // Make sure that we don't cause failures for JS Dev Errors that
+  // were expected, typically for tests that attempt to check
+  // that we react properly to TypeError, ReferenceError, SyntaxError.
+  if (cleanupRecentJSDevError && "clearRecentJSDevError" in ChromeUtils) {
+    let recentJSDevError = ChromeUtils.recentJSDevError;
+    if (recentJSDevError) {
+      if (expectedException(recentJSDevError)) {
+        ChromeUtils.clearRecentJSDevError();
+      }
+    }
+  }
 };
 
 /**
  * A promise that is expected to reject:
  * assert.rejects(promise, expected, message);
  *
  * @param promise
  *        (promise) A promise that is expected to reject