Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Allows a wildcard (%) to be specified when defining allowed globals.

Nathan Bowser7436707860e6
diff --git a/lib/runner.js b/lib/runner.js
index 1e7148a2..a55de90e 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -69,7 +69,7 @@ Runner.prototype.grep = function(re){
};
/**
- * Returns the number of tests matching the grep search for the
+ * Returns the number of tests matching the grep search for the
* given suite.
*
* @param {Suite} suite
@@ -113,9 +113,11 @@ Runner.prototype.globals = function(arr){
Runner.prototype.checkGlobals = function(test){
if (this.ignoreLeaks) return;
-
var leaks = utils.filter(utils.keys(global), function(key){
- return !~utils.indexOf(this._globals, key) && (!global.navigator || 'onerror' !== key);
+ var matched = utils.filter(this._globals, function(allowed){
+ return allowed == key || key.lastIndexOf(allowed.split('%')[0], 0) == 0;
+ });
+ return matched.length == 0 && (!global.navigator || 'onerror' !== key);
}, this);
this._globals = this._globals.concat(leaks);
diff --git a/test/runner.js b/test/runner.js
index 8fdcc92e..ba911e8a 100644
--- a/test/runner.js
+++ b/test/runner.js
@@ -46,6 +46,16 @@ describe('Runner', function(){
})
describe('.checkGlobals(test)', function(){
+ it('should allow variables that match a wildcard', function(done) {
+ runner.globals(['foo%', 'giz%']);
+ foo = 'baz';
+ gizmo = 'quux'
+ runner.checkGlobals();
+ delete global.foo;
+ delete global.gizmo;
+ done()
+ })
+
it('should emit "fail" when a new global is introduced', function(done){
runner.checkGlobals();
foo = 'bar';