Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Added global var leak reporting. Closes #12

Tj Holowaychuk1b983e2b13bd
diff --git a/lib/suite.js b/lib/suite.js
index 78f7d5dd..d61e8b33 100644
--- a/lib/suite.js
+++ b/lib/suite.js
@@ -26,6 +26,8 @@ function Suite(title) {
this.beforeEachCallbacks = [];
this.afterAllCallbacks = [];
this.afterEachCallbacks = [];
+ this.globals = Object.keys(global);
+ this.afterEach(this.checkGlobals.bind(this));
}
/**
@@ -34,6 +36,24 @@ 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
new file mode 100644
index 00000000..49c91a33
--- /dev/null
+++ b/test/unit/globals.js
@@ -0,0 +1,9 @@
+
+describe('global leaks', function(){
+ it('should cause tests to fail', function(){
+ // uncomment to test
+ // foo = 'bar';
+ // bar = 'baz';
+ // baz = 'raz';
+ });
+});
\ No newline at end of file