From 706139d9275d34774be509cec8eb9bed4a2c84e2 Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Mon, 5 Oct 2015 14:09:00 +0100 Subject: [PATCH] fix(ngMock): reset cache before every test We don't need to have values in the cache from previous tests. This was causing failures in all subsequent tests when a single test failed due to a memory leak. Now that we reset the cache each time we do not need to store the cache size at the start of each test Closes #13013 --- test/helpers/testabilityPatch.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/test/helpers/testabilityPatch.js b/test/helpers/testabilityPatch.js index acc8060dea25..469459c39094 100644 --- a/test/helpers/testabilityPatch.js +++ b/test/helpers/testabilityPatch.js @@ -1,4 +1,4 @@ -/* global jQuery: true, uid: true */ +/* global jQuery: true, uid: true, jqCache: true */ 'use strict'; /** @@ -12,6 +12,7 @@ if (window._jQuery) _jQuery.event.special.change = undefined; if (window.bindJQuery) bindJQuery(); beforeEach(function() { + // all this stuff is not needed for module tests, where jqlite and publishExternalAPI and jqLite are not global vars if (window.publishExternalAPI) { publishExternalAPI(angular); @@ -28,7 +29,10 @@ beforeEach(function() { // reset to jQuery or default to us. bindJQuery(); - jqLiteCacheSizeInit(); + + // Clear the cache to prevent memory leak failures from previous tests + // breaking subsequent tests unnecessarily + jqCache = jqLite.cache = {}; } angular.element(document.body).empty().removeData(); @@ -84,7 +88,6 @@ afterEach(function() { } } - // copied from Angular.js // we need this method here so that we can run module tests with wrapped angular.js function forEachSorted(obj, iterator, context) { @@ -133,14 +136,7 @@ function dealoc(obj) { function jqLiteCacheSize() { - var size = 0; - forEach(jqLite.cache, function() { size++; }); - return size - jqLiteCacheSize.initSize; -} -jqLiteCacheSize.initSize = 0; - -function jqLiteCacheSizeInit() { - jqLiteCacheSize.initSize = jqLiteCacheSize.initSize + jqLiteCacheSize(); + return Object.keys(jqLite.cache).length; }