Skip to content

Commit d03b1b3

Browse files
committed
Tests: Ensure no timers are running at the end of each test
This helps fix issues that make tooltip tests sometimes fail when run against jQuery 3.2 or newer due to timing differences. This commit adds a function to be attached at `afterEach` & attaches it to `common.testWidget`.
1 parent 9bb366e commit d03b1b3

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

tests/lib/common.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
define( [
22
"qunit",
3-
"jquery" ],
4-
function( QUnit, $ ) {
3+
"jquery",
4+
"lib/helper"
5+
], function( QUnit, $, helper ) {
56

67
var exports = {};
78

@@ -66,7 +67,7 @@ function testBasicUsage( widget ) {
6667
}
6768

6869
exports.testWidget = function( widget, settings ) {
69-
QUnit.module( widget + ": common widget" );
70+
QUnit.module( widget + ": common widget", { afterEach: helper.moduleAfterEach } );
7071

7172
exports.testJshint( "/widgets/" + widget );
7273
testWidgetDefaults( widget, settings.defaults );

tests/lib/helper.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ define( [
22
"jquery"
33
], function( $ ) {
44

5-
var exports = {};
5+
var exports = {},
6+
7+
// Store the old count so that we only assert on tests that have actually leaked,
8+
// instead of asserting every time a test has leaked sometime in the past
9+
oldActive = 0,
10+
splice = [].splice;
611

712
exports.forceScrollableWindow = function( appendTo ) {
813

@@ -28,6 +33,24 @@ exports.onFocus = function( element, onFocus ) {
2833
element.on( "focus", fn )[ 0 ].focus();
2934
};
3035

36+
/**
37+
* Ensures that tests have cleaned up properly after themselves. Should be passed as the
38+
* afterEach function on all modules' lifecycle object.
39+
*/
40+
exports.moduleAfterEach = function( assert ) {
41+
42+
// Check for (and clean up, if possible) incomplete animations/requests/etc.
43+
if ( jQuery.timers && jQuery.timers.length !== 0 ) {
44+
assert.equal( jQuery.timers.length, 0, "No timers are still running" );
45+
splice.call( jQuery.timers, 0, jQuery.timers.length );
46+
jQuery.fx.stop();
47+
}
48+
if ( jQuery.active !== undefined && jQuery.active !== oldActive ) {
49+
assert.equal( jQuery.active, oldActive, "No AJAX requests are still active" );
50+
oldActive = jQuery.active;
51+
}
52+
};
53+
3154
return exports;
3255

3356
} );

0 commit comments

Comments
 (0)