Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit fd83d37

Browse files
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
1 parent 2fcfd75 commit fd83d37

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

test/helpers/testabilityPatch.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global jQuery: true, uid: true */
1+
/* global jQuery: true, uid: true, jqCache: true */
22
'use strict';
33

44
/**
@@ -12,6 +12,7 @@ if (window._jQuery) _jQuery.event.special.change = undefined;
1212
if (window.bindJQuery) bindJQuery();
1313

1414
beforeEach(function() {
15+
1516
// all this stuff is not needed for module tests, where jqlite and publishExternalAPI and jqLite are not global vars
1617
if (window.publishExternalAPI) {
1718
publishExternalAPI(angular);
@@ -28,7 +29,10 @@ beforeEach(function() {
2829

2930
// reset to jQuery or default to us.
3031
bindJQuery();
31-
jqLiteCacheSizeInit();
32+
33+
// Clear the cache to prevent memory leak failures from previous tests
34+
// breaking subsequent tests unnecessarily
35+
jqCache = jqLite.cache = {};
3236
}
3337

3438
angular.element(document.body).empty().removeData();
@@ -84,7 +88,6 @@ afterEach(function() {
8488
}
8589
}
8690

87-
8891
// copied from Angular.js
8992
// we need this method here so that we can run module tests with wrapped angular.js
9093
function forEachSorted(obj, iterator, context) {
@@ -133,14 +136,7 @@ function dealoc(obj) {
133136

134137

135138
function jqLiteCacheSize() {
136-
var size = 0;
137-
forEach(jqLite.cache, function() { size++; });
138-
return size - jqLiteCacheSize.initSize;
139-
}
140-
jqLiteCacheSize.initSize = 0;
141-
142-
function jqLiteCacheSizeInit() {
143-
jqLiteCacheSize.initSize = jqLiteCacheSize.initSize + jqLiteCacheSize();
139+
return Object.keys(jqLite.cache).length;
144140
}
145141

146142

0 commit comments

Comments
 (0)