Bug 678120 - Browser-chrome mochitests should give a stacktrace on failures. r=ted.mielczarek
authorBlair McBride <bmcbride@mozilla.com>
Wed, 22 Sep 2010 20:58:42 +1200
changeset 76258 2470993f6287d3b594a542023ce1cb077fda9bff
parent 76026 61586623c897f4689c0e3c251608a23f1a14cb38
child 76259 26bc47cdea9e1177b9cbc155dbe60b6e02fd1b6b
push id3
push userfelipc@gmail.com
push dateFri, 30 Sep 2011 20:09:13 +0000
reviewersted
bugs678120
milestone9.0a1
Bug 678120 - Browser-chrome mochitests should give a stacktrace on failures. r=ted.mielczarek
testing/mochitest/browser-test.js
--- a/testing/mochitest/browser-test.js
+++ b/testing/mochitest/browser-test.js
@@ -310,17 +310,17 @@ Tester.prototype = {
     if (aIID.equals(Ci.nsIConsoleListener) ||
         aIID.equals(Ci.nsISupports))
       return this;
 
     throw Components.results.NS_ERROR_NO_INTERFACE;
   }
 };
 
-function testResult(aCondition, aName, aDiag, aIsTodo) {
+function testResult(aCondition, aName, aDiag, aIsTodo, aStack) {
   this.msg = aName || "";
 
   this.info = false;
   this.pass = !!aCondition;
   this.todo = aIsTodo;
 
   if (this.pass) {
     if (aIsTodo)
@@ -330,16 +330,24 @@ function testResult(aCondition, aName, a
   } else {
     if (aDiag) {
       if (typeof aDiag == "object" && "fileName" in aDiag) {
         // we have an exception - print filename and linenumber information
         this.msg += " at " + aDiag.fileName + ":" + aDiag.lineNumber;
       }
       this.msg += " - " + aDiag;
     }
+    if (aStack) {
+      this.msg += "\nStack trace:\n";
+      var frame = aStack;
+      while (frame) {
+        this.msg += "    " + frame + "\n";
+        frame = frame.caller;
+      }
+    }
     if (aIsTodo)
       this.result = "TEST-UNEXPECTED-PASS";
     else
       this.result = "TEST-UNEXPECTED-FAIL";
   }
 }
 
 function testMessage(aName) {
@@ -350,33 +358,39 @@ function testMessage(aName) {
 
 // Need to be careful adding properties to this object, since its properties
 // cannot conflict with global variables used in tests.
 function testScope(aTester, aTest) {
   this.__tester = aTester;
   this.__browserTest = aTest;
 
   var self = this;
-  this.ok = function test_ok(condition, name, diag) {
-    self.__browserTest.addResult(new testResult(condition, name, diag, false));
+  this.ok = function test_ok(condition, name, diag, stack) {
+    self.__browserTest.addResult(new testResult(condition, name, diag, false,
+                                                stack ? stack : Components.stack.caller));
   };
   this.is = function test_is(a, b, name) {
-    self.ok(a == b, name, "Got " + a + ", expected " + b);
+    self.ok(a == b, name, "Got " + a + ", expected " + b, false,
+            Components.stack.caller);
   };
   this.isnot = function test_isnot(a, b, name) {
-    self.ok(a != b, name, "Didn't expect " + a + ", but got it");
+    self.ok(a != b, name, "Didn't expect " + a + ", but got it", false,
+            Components.stack.caller);
   };
-  this.todo = function test_todo(condition, name, diag) {
-    self.__browserTest.addResult(new testResult(!condition, name, diag, true));
+  this.todo = function test_todo(condition, name, diag, stack) {
+    self.__browserTest.addResult(new testResult(!condition, name, diag, true,
+                                                stack ? stack : Components.stack.caller));
   };
   this.todo_is = function test_todo_is(a, b, name) {
-    self.todo(a == b, name, "Got " + a + ", expected " + b);
+    self.todo(a == b, name, "Got " + a + ", expected " + b,
+              Components.stack.caller);
   };
   this.todo_isnot = function test_todo_isnot(a, b, name) {
-    self.todo(a != b, name, "Didn't expect " + a + ", but got it");
+    self.todo(a != b, name, "Didn't expect " + a + ", but got it",
+              Components.stack.caller);
   };
   this.info = function test_info(name) {
     self.__browserTest.addResult(new testMessage(name));
   };
 
   this.executeSoon = function test_executeSoon(func) {
     let tm = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);