Home page Forum Wiki Mail Jabber Devel NIX-FILES

AltLUG Git

Git-repositories of Altai Linux User Group

Improve extractStacktrace() implementation. Fixes #254 - Include all relevant stack...
authorJörn Zaefferer <joern.zaefferer@gmail.com>
Mon, 4 Jun 2012 10:58:43 +0000 (12:58 +0200)
committerJörn Zaefferer <joern.zaefferer@gmail.com>
Mon, 4 Jun 2012 11:00:11 +0000 (13:00 +0200)
qunit/qunit.js

index 3b78401..97d808d 100644 (file)
@@ -13,6 +13,7 @@
 var QUnit,
        config,
        testId = 0,
+       fileName = (sourceFromStacktrace( 0 ) || "" ).replace(/(:\d+)+\)?/, "").replace(/.+\//, ""),
        toString = Object.prototype.toString,
        hasOwn = Object.prototype.hasOwnProperty,
        defined = {
@@ -131,7 +132,7 @@ Test.prototype = {
                try {
                        this.callback.call( this.testEnvironment, QUnit.assert );
                } catch( e ) {
-                       QUnit.pushFailure( "Died on test #" + (this.assertions.length + 1) + ": " + e.message, extractStacktrace( e, 1 ) );
+                       QUnit.pushFailure( "Died on test #" + (this.assertions.length + 1) + " " + this.stack + ": " + e.message, extractStacktrace( e, 0 ) );
                        // else next test will carry the responsibility
                        saveGlobal();
 
@@ -1036,9 +1037,9 @@ function validTest( test ) {
 // Later Safari and IE10 are supposed to support error.stack as well
 // See also https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error/Stack
 function extractStacktrace( e, offset ) {
-       offset = offset || 3;
+       offset = offset === undefined ? 3 : offset;
 
-       var stack;
+       var stack, include, i, regex;
 
        if ( e.stacktrace ) {
                // Opera
@@ -1049,6 +1050,18 @@ function extractStacktrace( e, offset ) {
                if (/^error$/i.test( stack[0] ) ) {
                        stack.shift();
                }
+               if ( fileName ) {
+                       include = [];
+                       for ( i = offset; i < stack.length; i++ ) {
+                               if ( stack[ i ].indexOf( fileName ) != -1 ) {
+                                       break;
+                               }
+                               include.push( stack[ i ] );
+                       }
+                       if ( include.length ) {
+                               return include.join( "\n" );
+                       }
+               }
                return stack[ offset ];
        } else if ( e.sourceURL ) {
                // Safari, PhantomJS

Hosting is provided AltSPAHow to use Git
Please direct any questions on jabber alex.wolf@jabber.ru

Valid HTML 4.0 Transitional Valid CSS!

Designer Alexander Wolf