Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

add faster .length checks to checkGlobals() before performing the filter

TJ Holowaychuk747588f92d11
diff --git a/lib/runner.js b/lib/runner.js
index a20cd089..6e77ecdf 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -119,8 +119,16 @@ Runner.prototype.globals = function(arr){
Runner.prototype.checkGlobals = function(test){
if (this.ignoreLeaks) return;
- var leaks = filterLeaks(this._globals);
+ var ok = this._globals;
+ var globals = keys(global);
+ var isNode = process.kill;
+ var leaks;
+ // check length - 2 ('errno' and 'location' globals)
+ if (isNode && 1 == ok.length - globals.length) return
+ else if (2 == ok.length - globals.length) return;
+
+ leaks = filterLeaks(ok, globals);
this._globals = this._globals.concat(leaks);
if (leaks.length > 1) {
@@ -472,12 +480,13 @@ Runner.prototype.run = function(fn){
* Filter leaks with the given globals flagged as `ok`.
*
* @param {Array} ok
+ * @param {Array} globals
* @return {Array}
* @api private
*/
-function filterLeaks(ok) {
- return filter(keys(global), function(key){
+function filterLeaks(ok, globals) {
+ return filter(globals, function(key){
var matched = filter(ok, function(ok){
if (~ok.indexOf('*')) return 0 == key.indexOf(ok.split('*')[0]);
return key == ok;
diff --git a/mocha.js b/mocha.js
index f9c149e9..f67fd026 100644
--- a/mocha.js
+++ b/mocha.js
@@ -3752,8 +3752,16 @@ Runner.prototype.globals = function(arr){
Runner.prototype.checkGlobals = function(test){
if (this.ignoreLeaks) return;
- var leaks = filterLeaks(this._globals);
+ var ok = this._globals;
+ var globals = keys(global);
+ var isNode = process.kill;
+ var leaks;
+ // check length - 2 ('errno' and 'location' globals)
+ if (isNode && 1 == ok.length - globals.length) return
+ else if (2 == ok.length - globals.length) return;
+
+ leaks = filterLeaks(ok, globals);
this._globals = this._globals.concat(leaks);
if (leaks.length > 1) {
@@ -4105,12 +4113,13 @@ Runner.prototype.run = function(fn){
* Filter leaks with the given globals flagged as `ok`.
*
* @param {Array} ok
+ * @param {Array} globals
* @return {Array}
* @api private
*/
-function filterLeaks(ok) {
- return filter(keys(global), function(key){
+function filterLeaks(ok, globals) {
+ return filter(globals, function(key){
var matched = filter(ok, function(ok){
if (~ok.indexOf('*')) return 0 == key.indexOf(ok.split('*')[0]);
return key == ok;
diff --git a/test/browser/large.html b/test/browser/large.html
index 96ec23ad..edfba9eb 100644
--- a/test/browser/large.html
+++ b/test/browser/large.html
@@ -13,7 +13,7 @@
<script src="large.js"></script>
<script>
onload = function(){
- mocha.ignoreLeaks().run();
+ mocha.run();
};
</script>
</head>