qunit: Improved unfinished ajax detection
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 5 Aug 2015 00:11:11 +0000 (17:11 -0700)
committerTimo Tijhof <krinklemail@gmail.com>
Wed, 5 Aug 2015 17:54:25 +0000 (10:54 -0700)
Sometimes $.active is non-zero but the ajaxRequests array doesn't
contain any pending requests (yet). In that case, log $.active
as well.

Change-Id: I1c181b1234d1f883605de38805bc029c064a1f5b

tests/qunit/data/testrunner.js

index 41754b4..78771de 100644 (file)
                                },
 
                                teardown: function () {
-                                       var timers, active;
+                                       var timers, pending, $activeLen;
 
                                        localEnv.teardown.call( this );
 
                                        }
 
                                        // Test should use fake XHR, wait for requests, or call abort()
-                                       if ( $.active !== undefined && $.active !== 0 ) {
-                                               active = $.grep( ajaxRequests, function ( ajax ) {
+                                       $activeLen = $.active;
+                                       if ( $activeLen !== undefined && $activeLen !== 0 ) {
+                                               pending = $.grep( ajaxRequests, function ( ajax ) {
                                                        return ajax.xhr.state() === 'pending';
                                                } );
-                                               if ( active.length !== $.active ) {
+                                               if ( pending.length !== $activeLen ) {
                                                        mw.log.warn( 'Pending requests does not match jQuery.active count' );
                                                }
                                                // Force requests to stop to give the next test a clean start
-                                               $.each( active, function ( i, ajax ) {
-                                                       mw.log.warn( 'Unfinished AJAX request #' + i, ajax.options );
+                                               $.each( pending, function ( i, ajax ) {
+                                                       mw.log.warn( 'Pending AJAX request #' + i, ajax.options );
                                                        ajax.xhr.abort();
                                                } );
                                                ajaxRequests = [];
 
-                                               throw new Error( 'Unfinished AJAX requests: ' + active.length );
+                                               throw new Error( 'Pending AJAX requests: ' + pending.length + ' (active: ' + $activeLen + ')' );
                                        }
                                }
                        };