Skip to content

Tests: Ensure no timers are running at the end of each test #1920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions tests/lib/common.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
define( [
"qunit",
"jquery" ],
function( QUnit, $ ) {
"jquery",
"lib/helper"
], function( QUnit, $, helper ) {

var exports = {};

Expand Down Expand Up @@ -66,7 +67,7 @@ function testBasicUsage( widget ) {
}

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

exports.testJshint( "/widgets/" + widget );
testWidgetDefaults( widget, settings.defaults );
Expand Down
25 changes: 24 additions & 1 deletion tests/lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ define( [
"jquery"
], function( $ ) {

var exports = {};
var exports = {},

// Store the old count so that we only assert on tests that have actually leaked,
// instead of asserting every time a test has leaked sometime in the past
oldActive = 0,
splice = [].splice;

exports.forceScrollableWindow = function( appendTo ) {

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

/**
* Ensures that tests have cleaned up properly after themselves. Should be passed as the
* afterEach function on all modules' lifecycle object.
*/
exports.moduleAfterEach = function( assert ) {

// Check for (and clean up, if possible) incomplete animations/requests/etc.
if ( jQuery.timers && jQuery.timers.length !== 0 ) {
assert.equal( jQuery.timers.length, 0, "No timers are still running" );
splice.call( jQuery.timers, 0, jQuery.timers.length );
jQuery.fx.stop();
}
if ( jQuery.active !== undefined && jQuery.active !== oldActive ) {
assert.equal( jQuery.active, oldActive, "No AJAX requests are still active" );
oldActive = jQuery.active;
}
};

return exports;

} );
4 changes: 2 additions & 2 deletions tests/unit/accordion/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ define( [
"ui/widgets/accordion"
], function( QUnit, $, testHelper ) {

var setupTeardown = testHelper.setupTeardown,
var beforeAfterEach = testHelper.beforeAfterEach,
state = testHelper.state;

QUnit.module( "accordion: core", setupTeardown() );
QUnit.module( "accordion: core", beforeAfterEach() );

$.each( { div: "#list1", ul: "#navigation", dl: "#accordion-dl" }, function( type, selector ) {
QUnit.test( "markup structure: " + type, function( assert ) {
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/accordion/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ define( [
"ui/widgets/accordion"
], function( QUnit, $, testHelper ) {

var setupTeardown = testHelper.setupTeardown,
var beforeAfterEach = testHelper.beforeAfterEach,
state = testHelper.state;

QUnit.module( "accordion: events", setupTeardown() );
QUnit.module( "accordion: events", beforeAfterEach() );

QUnit.test( "create", function( assert ) {
assert.expect( 10 );
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/accordion/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ return $.extend( helper, {
} );
},

setupTeardown: function() {
beforeAfterEach: function() {
var animate = $.ui.accordion.prototype.options.animate;
return {
setup: function() {
beforeEach: function() {
$.ui.accordion.prototype.options.animate = false;
},
teardown: function() {
afterEach: function() {
$.ui.accordion.prototype.options.animate = animate;
return helper.moduleAfterEach.apply( this, arguments );
}
};
},
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/accordion/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ define( [
], function( QUnit, $, testHelper ) {

var equalHeight = testHelper.equalHeight,
setupTeardown = testHelper.setupTeardown,
beforeAfterEach = testHelper.beforeAfterEach,
state = testHelper.state;

QUnit.module( "accordion: methods", setupTeardown() );
QUnit.module( "accordion: methods", beforeAfterEach() );

QUnit.test( "destroy", function( assert ) {
assert.expect( 1 );
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/accordion/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ define( [
], function( QUnit, $, testHelper ) {

var equalHeight = testHelper.equalHeight,
setupTeardown = testHelper.setupTeardown,
beforeAfterEach = testHelper.beforeAfterEach,
state = testHelper.state;

QUnit.module( "accordion: options", setupTeardown() );
QUnit.module( "accordion: options", beforeAfterEach() );

QUnit.test( "{ active: default }", function( assert ) {
assert.expect( 2 );
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/autocomplete/core.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/autocomplete"
], function( QUnit, $ ) {
], function( QUnit, $, helper ) {

QUnit.module( "autocomplete: core" );
QUnit.module( "autocomplete: core", { afterEach: helper.moduleAfterEach } );

QUnit.test( "markup structure", function( assert ) {
assert.expect( 2 );
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/autocomplete/events.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/autocomplete"
], function( QUnit, $ ) {
], function( QUnit, $, helper ) {

QUnit.module( "autocomplete: events" );
QUnit.module( "autocomplete: events", { afterEach: helper.moduleAfterEach } );

var data = [ "Clojure", "COBOL", "ColdFusion", "Java", "JavaScript", "Scala", "Scheme" ];

Expand Down
5 changes: 3 additions & 2 deletions tests/unit/autocomplete/methods.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/autocomplete"
], function( QUnit, $ ) {
], function( QUnit, $, helper ) {

QUnit.module( "autocomplete: methods" );
QUnit.module( "autocomplete: methods", { afterEach: helper.moduleAfterEach } );

QUnit.test( "destroy", function( assert ) {
assert.expect( 1 );
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/autocomplete/options.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/autocomplete"
], function( QUnit, $ ) {
], function( QUnit, $, helper ) {

QUnit.module( "autocomplete: options" );
QUnit.module( "autocomplete: options", { afterEach: helper.moduleAfterEach } );

var data = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby",
"python", "c", "scala", "groovy", "haskell", "perl" ];
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/button/core.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
define( [
"qunit",
"jquery",
"lib/helper",
"ui/safe-active-element",
"ui/widgets/button"
], function( QUnit, $ ) {
], function( QUnit, $, helper ) {

QUnit.module( "Button: core" );
QUnit.module( "Button: core", { afterEach: helper.moduleAfterEach } );

QUnit.test( "Disabled button loses focus", function( assert ) {
var ready = assert.async();
Expand Down
9 changes: 5 additions & 4 deletions tests/unit/button/deprecated.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/button"
], function( QUnit, $ ) {
], function( QUnit, $, helper ) {

QUnit.module( "Button (deprecated): core" );
QUnit.module( "Button (deprecated): core", { afterEach: helper.moduleAfterEach } );

QUnit.test( "Calling button on a checkbox input calls checkboxradio widget", function( assert ) {
var checkbox = $( "#checkbox01" );
Expand All @@ -27,7 +28,7 @@ QUnit.test( "Calling buttonset calls controlgroup", function( assert ) {
assert.ok( controlgroup.is( ":ui-controlgroup" ), "Calling buttonset creates controlgroup instance" );
} );

QUnit.module( "Button (deprecated): methods" );
QUnit.module( "Button (deprecated): methods", { afterEach: helper.moduleAfterEach } );

QUnit.test( "destroy", function( assert ) {
assert.expect( 1 );
Expand Down Expand Up @@ -59,7 +60,7 @@ QUnit.test( "refresh: Ensure disabled state is preserved correctly.", function(

} );

QUnit.module( "button (deprecated): options" );
QUnit.module( "button (deprecated): options", { afterEach: helper.moduleAfterEach } );

QUnit.test( "Setting items option on buttonset sets the button properties on the items option", function( assert ) {
assert.expect( 2 );
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/button/events.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/button"
], function( QUnit, $ ) {
], function( QUnit, $, helper ) {

QUnit.module( "Button: events" );
QUnit.module( "Button: events", { afterEach: helper.moduleAfterEach } );

QUnit.test( "Anchor recieves click event when spacebar is pressed", function( assert ) {
var ready = assert.async();
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/button/methods.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/button"
], function( QUnit, $ ) {
], function( QUnit, $, helper ) {

QUnit.module( "Button: methods" );
QUnit.module( "Button: methods", { afterEach: helper.moduleAfterEach } );

QUnit.test( "destroy", function( assert ) {
assert.expect( 1 );
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/button/options.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/button"
], function( QUnit, $ ) {
], function( QUnit, $, helper ) {

QUnit.module( "button: options" );
QUnit.module( "button: options", { afterEach: helper.moduleAfterEach } );

QUnit.test( "disabled, explicit value", function( assert ) {
assert.expect( 8 );
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/checkboxradio/core.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/checkboxradio"
], function( QUnit, $ ) {
], function( QUnit, $, helper ) {

QUnit.module( "Checkboxradio: core" );
QUnit.module( "Checkboxradio: core", { afterEach: helper.moduleAfterEach } );

QUnit.test( "Checkbox - Initial class structure", function( assert ) {
assert.expect( 2 );
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/checkboxradio/events.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/checkboxradio"
], function( QUnit, $ ) {
], function( QUnit, $, helper ) {

QUnit.module( "Checkboxradio: events" );
QUnit.module( "Checkboxradio: events", { afterEach: helper.moduleAfterEach } );

QUnit.test(
"Resetting a checkbox's form should refresh the visual state of the checkbox",
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/checkboxradio/methods.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/checkboxradio"
], function( QUnit, $ ) {
], function( QUnit, $, helper ) {

QUnit.module( "Checkboxradio: methods" );
QUnit.module( "Checkboxradio: methods", { afterEach: helper.moduleAfterEach } );

$.each( [ "checkbox", "radio" ], function( index, value ) {
QUnit.test( value + ": refresh", function( assert ) {
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/checkboxradio/options.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/checkboxradio"
], function( QUnit, $ ) {
], function( QUnit, $, helper ) {

QUnit.module( "Checkboxradio: options" );
QUnit.module( "Checkboxradio: options", { afterEach: helper.moduleAfterEach } );

function assertDisabled( checkbox, assert ) {
assert.hasClasses( checkbox.checkboxradio( "widget" ), "ui-state-disabled",
Expand Down
9 changes: 5 additions & 4 deletions tests/unit/controlgroup/core.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/controlgroup",
"ui/widgets/checkboxradio",
"ui/widgets/selectmenu",
"ui/widgets/button",
"ui/widgets/spinner"
], function( QUnit, $ ) {
], function( QUnit, $, helper ) {

QUnit.module( "Controlgroup: Core" );
QUnit.module( "Controlgroup: Core", { afterEach: helper.moduleAfterEach } );

QUnit.test( "selectmenu: open/close corners", function( assert ) {
assert.expect( 12 );
Expand Down Expand Up @@ -161,12 +162,12 @@ QUnit.test( "Single controlgroup button - vertical", function( assert ) {
} );

QUnit.module( "Controlgroup: Non-empty class key", {
setup: function() {
beforeEach: function() {
this.classKey = $.ui.selectmenu.prototype.options.classes[ "ui-selectmenu-button-closed" ];
$.ui.selectmenu.prototype.options.classes[ "ui-selectmenu-button-closed" ] =
"something-custom";
},
teardown: function() {
afterEach: function() {
$.ui.selectmenu.prototype.options.classes[ "ui-selectmenu-button-closed" ] = this.classKey;
}
} );
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/controlgroup/methods.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/controlgroup",
"ui/widgets/checkboxradio",
"ui/widgets/selectmenu",
"ui/widgets/button",
"ui/widgets/spinner"
], function( QUnit, $ ) {
], function( QUnit, $, helper ) {

QUnit.module( "Controlgroup: methods" );
QUnit.module( "Controlgroup: methods", { afterEach: helper.moduleAfterEach } );

QUnit.test( "destroy", function( assert ) {
assert.expect( 1 );
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/controlgroup/options.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/controlgroup",
"ui/widgets/checkboxradio",
"ui/widgets/selectmenu",
"ui/widgets/button",
"ui/widgets/spinner"
], function( QUnit, $ ) {
], function( QUnit, $, helper ) {

QUnit.module( "Controlgroup: options" );
QUnit.module( "Controlgroup: options", { afterEach: helper.moduleAfterEach } );

QUnit.test( "disabled", function( assert ) {
assert.expect( 4 );
Expand Down
Loading