Bug 1237961 - Assert.throws raises a TypeError exception when the "expected" parameter is an arrow function. r=mikedeboer draft
authorPaolo Amadini <paolo.mozmail@amadzone.org>
Fri, 20 Jan 2017 15:38:56 +0100
changeset 464160 ce7db1003a450d81a4aa0eea42a6f05da9e8e7e8
parent 464075 aa3e49299a3aa5cb0db570532e3df9e75d30c2d1
child 542872 e4f51c038f29f0cf400b7a94bebf202e1d872b23
push id42292
push userpaolo.mozmail@amadzone.org
push dateFri, 20 Jan 2017 14:39:54 +0000
reviewersmikedeboer
bugs1237961
milestone53.0a1
Bug 1237961 - Assert.throws raises a TypeError exception when the "expected" parameter is an arrow function. r=mikedeboer MozReview-Commit-ID: CyhD00Uwbwj
testing/modules/Assert.jsm
testing/modules/tests/xpcshell/test_assert.js
--- a/testing/modules/Assert.jsm
+++ b/testing/modules/Assert.jsm
@@ -309,17 +309,20 @@ proto.notStrictEqual = function notStric
 
 function expectedException(actual, expected) {
   if (!actual || !expected) {
     return false;
   }
 
   if (instanceOf(expected, "RegExp")) {
     return expected.test(actual);
-  } else if (actual instanceof expected) {
+  // We need to guard against the right hand parameter of "instanceof" lacking
+  // the "prototype" property, which is true of arrow functions in particular.
+  } else if (!(typeof expected === "function" && !expected.prototype) &&
+             actual instanceof expected) {
     return true;
   } else if (expected.call({}, actual) === true) {
     return true;
   }
 
   return false;
 }
 
--- a/testing/modules/tests/xpcshell/test_assert.js
+++ b/testing/modules/tests/xpcshell/test_assert.js
@@ -203,16 +203,22 @@ function run_test() {
   assert.throws(makeBlock(thrower, TypeError), /test/);
 
   // use a fn to validate error object
   assert.throws(makeBlock(thrower, TypeError), function(err) {
     if ((err instanceof TypeError) && /test/.test(err)) {
       return true;
     }
   });
+  // do the same with an arrow function
+  assert.throws(makeBlock(thrower, TypeError), err => {
+    if ((err instanceof TypeError) && /test/.test(err)) {
+      return true;
+    }
+  });
 
   function testAssertionMessage(actual, expected) {
     try {
       assert.equal(actual, "");
     } catch (e) {
       assert.equal(e.toString(),
           ["AssertionError:", expected, "==", '""'].join(" "));
     }