Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Added --globals <names> to specify accepted globals. Closes #99

TJ Holowaychuka021710257cd
diff --git a/bin/mocha b/bin/mocha
index a50bd40f..9ce9e102 100755
--- a/bin/mocha
+++ b/bin/mocha
@@ -45,7 +45,8 @@ program
.option('-s, --slow <ms>', '"slow" test threshold in milliseconds [75]', parseInt)
.option('-C, --no-colors', 'force disabling of colors')
.option('-c, --colors', 'force enabling of colors')
- .option('-G, --growl', 'enable growl support')
+ .option('-G, --growl', 'enable growl notification support')
+ .option('--globals <names>', 'allow the given comma-delimited global [names]', list)
.option('--interfaces', 'display available interfaces')
.option('--reporters', 'display available reporters')
@@ -171,6 +172,7 @@ function run(suite) {
suite.emit('run');
var runner = new Runner(suite);
var reporter = new Reporter(runner);
+ runner.globals(program.globals);
if (program.grep) runner.grep(new RegExp(program.grep));
if (program.growl) growl(runner, reporter)
runner.run();
@@ -217,3 +219,9 @@ function notify(msg, options) {
if (options.title) args.push(options.title);
exec(args.join(' '), fn);
}
+
+// parse comma delimited list
+
+function list(str) {
+ return str.split(/ *, */);
+}
\ No newline at end of file
diff --git a/lib/runner.js b/lib/runner.js
index 362c269c..d5e705fd 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -34,12 +34,13 @@ module.exports = Runner;
function Runner(suite) {
var self = this;
+ this._globals = [];
this.suite = suite;
this.total = suite.total();
- this.globals = Object.keys(global).concat(['errno']);
this.on('test end', function(test){ self.checkGlobals(test); });
this.on('hook end', function(hook){ self.checkGlobals(hook); });
this.grep(/.*/);
+ this.globals(Object.keys(global).concat(['errno']));
}
/**
@@ -61,6 +62,21 @@ Runner.prototype.grep = function(re){
return this;
};
+/**
+ * Allow the given `arr` of globals.
+ *
+ * @param {Array} arr
+ * @return {Runner} for chaining
+ * @api public
+ */
+
+Runner.prototype.globals = function(arr){
+ arr.forEach(function(arr){
+ this._globals.push(arr);
+ }, this);
+ return this;
+};
+
/**
* Check for global variable leaks.
*
@@ -69,10 +85,10 @@ Runner.prototype.grep = function(re){
Runner.prototype.checkGlobals = function(test){
var leaks = Object.keys(global).filter(function(key){
- return !~this.globals.indexOf(key);
+ return !~this._globals.indexOf(key);
}, this);
- this.globals = this.globals.concat(leaks);
+ this._globals = this._globals.concat(leaks);
if (leaks.length > 1) {
this.fail(test, new Error('global leaks detected: ' + leaks.join(', ') + ''));
diff --git a/test/globals.js b/test/globals.js
index d96e1c71..6ba173cc 100644
--- a/test/globals.js
+++ b/test/globals.js
@@ -17,7 +17,12 @@ describe('global leaks', function(){
// bar = 'baz';
// baz = 'raz';
});
-
+
+ it('should pass when accepted', function(){
+ okGlobalA = 1;
+ okGlobalB = 1;
+ })
+
afterEach(function(){
// uncomment to test
// foo = 'bar'
diff --git a/test/mocha.opts b/test/mocha.opts
index 7104117c..d900fff0 100644
--- a/test/mocha.opts
+++ b/test/mocha.opts
@@ -2,3 +2,4 @@
--reporter dot
--ui bdd
--growl
+--globals okGlobalA,okGlobalB
\ No newline at end of file