diff options
author | Colin Snover <github.com@zetafleet.com> | 2011-01-09 15:58:47 -0600 |
---|---|---|
committer | Colin Snover <github.com@zetafleet.com> | 2011-01-09 15:58:47 -0600 |
commit | e2941d5a98e91c5f61b200b2763e5fa0eb339365 (patch) | |
tree | 6bcc82b8589f18aacf734ba80efe72108e935c84 /test/data | |
parent | 80af46e8ffe8292e0af0537db6c7e89019e5edba (diff) | |
download | jquery-e2941d5a98e91c5f61b200b2763e5fa0eb339365.tar.gz jquery-e2941d5a98e91c5f61b200b2763e5fa0eb339365.zip |
Update unit tests with a leak detection mechanism for the various jQuery globals and fix all leaks in the tests.
Diffstat (limited to 'test/data')
-rw-r--r-- | test/data/testinit.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/test/data/testinit.js b/test/data/testinit.js index a66f71d25..c478390d5 100644 --- a/test/data/testinit.js +++ b/test/data/testinit.js @@ -45,3 +45,52 @@ function t(a,b,c) { function url(value) { return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000); } + +(function () { + // Store the old counts so that we only assert on tests that have actually leaked, + // instead of asserting every time a test has leaked sometime in the past + var oldCacheLength = 0, + oldFragmentsLength = 0, + oldTimersLength = 0, + oldActive = 0; + + /** + * Ensures that tests have cleaned up properly after themselves. Should be passed as the + * teardown function on all modules' lifecycle object. + */ + this.moduleTeardown = function () { + var i, fragmentsLength = 0, cacheLength = 0; + + // Allow QUnit.reset to clean up any attached elements before checking for leaks + QUnit.reset(); + + for ( i in jQuery.cache ) { + ++cacheLength; + } + + jQuery.fragments = {}; + + for ( i in jQuery.fragments ) { + ++fragmentsLength; + } + + // Because QUnit doesn't have a mechanism for retrieving the number of expected assertions for a test, + // if we unconditionally assert any of these, the test will fail with too many assertions :| + if ( cacheLength !== oldCacheLength ) { + equals( cacheLength, oldCacheLength, "No unit tests leak memory in jQuery.cache" ); + oldCacheLength = cacheLength; + } + if ( fragmentsLength !== oldFragmentsLength ) { + equals( fragmentsLength, oldFragmentsLength, "No unit tests leak memory in jQuery.fragments" ); + oldFragmentsLength = fragmentsLength; + } + if ( jQuery.timers.length !== oldTimersLength ) { + equals( jQuery.timers.length, oldTimersLength, "No timers are still running" ); + oldTimersLength = jQuery.timers.length; + } + if ( jQuery.active !== oldActive ) { + equals( jQuery.active, 0, "No AJAX requests are still active" ); + oldActive = jQuery.active; + } + } +}());
\ No newline at end of file |