Skip to content

armgit / mocha

Public Imported from GitHub
0

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

moved global var leak to test Runner

Tj Holowaychukc3aaca1766e1
diff --git a/lib/runner.js b/lib/runner.js
index fddccc32..12e640d0 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -33,6 +33,8 @@ module.exports = Runner;
function Runner(suite) {
this.suite = suite;
this.total = suite.total();
+ this.globals = Object.keys(global);
+ this.on('test end', this.checkGlobals.bind(this));
}
/**
@@ -41,6 +43,26 @@ function Runner(suite) {
Runner.prototype.__proto__ = EventEmitter.prototype;
+/**
+ * Check for global variable leaks.
+ *
+ * @api private
+ */
+
+Runner.prototype.checkGlobals = function(test){
+ var leaks = Object.keys(global).filter(function(key){
+ return !~this.globals.indexOf(key);
+ }, this);
+
+ this.globals = this.globals.concat(leaks);
+
+ if (leaks.length > 1) {
+ this.fail(test, new Error('global leaks detected: ' + leaks.join(', ') + ''));
+ } else if (leaks.length) {
+ this.fail(test, new Error('global leak detected: ' + leaks[0]));
+ }
+};
+
/**
* Fail the given `test`.
*
diff --git a/lib/suite.js b/lib/suite.js
index d61e8b33..78f7d5dd 100644
--- a/lib/suite.js
+++ b/lib/suite.js
@@ -26,8 +26,6 @@ function Suite(title) {
this.beforeEachCallbacks = [];
this.afterAllCallbacks = [];
this.afterEachCallbacks = [];
- this.globals = Object.keys(global);
- this.afterEach(this.checkGlobals.bind(this));
}
/**
@@ -36,24 +34,6 @@ function Suite(title) {
Suite.prototype.__proto__ = EventEmitter.prototype;
-/**
- * Check for global variable leaks.
- *
- * @api private
- */
-
-Suite.prototype.checkGlobals = function(){
- var leaks = Object.keys(global).filter(function(key){
- return !~this.globals.indexOf(key);
- }, this);
-
- if (leaks.length > 1) {
- throw new Error('global leaks detected: ' + leaks.join(', ') + '');
- } else if (leaks.length) {
- throw new Error('global leak detected: ' + leaks[0]);
- }
-};
-
/**
* Run `fn(test[, done])` before running tests.
*
diff --git a/test/unit/globals.js b/test/unit/globals.js
index a20f4c3a..83a5cbac 100644
--- a/test/unit/globals.js
+++ b/test/unit/globals.js
@@ -8,7 +8,7 @@ describe('global leaks', function(){
it('should cause tests to fail', function(){
// uncomment to test
// foo = 'bar';
- // bar = 'baz';
+ bar = 'baz';
// baz = 'raz';
});
});
\ No newline at end of file