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
--- 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