Skip to content

armgit / mocha

Public Imported from GitHub
0

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

ocd

TJ Holowaychukda0415303ead
diff --git a/lib/runner.js b/lib/runner.js
index 9f550879..e8ba2640 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -65,7 +65,6 @@ Runner.prototype.grep = function(re){
debug('grep %s', re);
this._grep = re;
this.total = this.grepTotal(this.suite);
-
return this;
};
@@ -83,9 +82,7 @@ Runner.prototype.grepTotal = function(suite) {
var total = 0;
suite.eachTest(function(test){
- if (self._grep.test(test.fullTitle())){
- total++;
- };
+ if (self._grep.test(test.fullTitle())) total++;
});
return total;
diff --git a/mocha.js b/mocha.js
index a2fe75df..d3d4af4e 100644
--- a/mocha.js
+++ b/mocha.js
@@ -2450,8 +2450,9 @@ function Progress(runner, options) {
// tests complete
runner.on('test end', function(){
+ complete++;
var incomplete = total - complete
- , percent = complete++ / total
+ , percent = complete / total
, n = width * percent | 0
, i = width - n;
@@ -3039,7 +3040,8 @@ Runner.prototype.constructor = Runner;
/**
- * Run tests with full titles matching `re`.
+ * Run tests with full titles matching `re`. Updates runner.total
+ * with number of tests matched.
*
* @param {RegExp} re
* @return {Runner} for chaining
@@ -3049,9 +3051,33 @@ Runner.prototype.constructor = Runner;
Runner.prototype.grep = function(re){
debug('grep %s', re);
this._grep = re;
+ this.total = this.grepTotal(this.suite);
+
return this;
};
+/**
+ * Returns the number of tests matching the grep search for the
+ * given suite.
+ *
+ * @param {Suite} suite
+ * @return {Number}
+ * @api public
+ */
+
+Runner.prototype.grepTotal = function(suite) {
+ var self = this;
+ var total = 0;
+
+ suite.eachTest(function(test){
+ if (self._grep.test(test.fullTitle())){
+ total++;
+ };
+ });
+
+ return total;
+};
+
/**
* Allow the given `arr` of globals.
*
@@ -3332,7 +3358,10 @@ Runner.prototype.runSuite = function(suite, fn){
, i = 0;
debug('run suite %s', suite.fullTitle());
- this.emit('suite', this.suite = suite);
+
+ if(self.grepTotal(suite)) {
+ this.emit('suite', this.suite = suite);
+ }
function next() {
var curr = suite.suites[i++];
@@ -3669,6 +3698,24 @@ Suite.prototype.total = function(){
}, 0) + this.tests.length;
};
+/**
+ * Iterates through each suite recursively to find
+ * all tests. Applies a function in the format
+ * `fn(test)`.
+ *
+ * @param {Function} fn
+ * @return {Suite}
+ * @api private
+ */
+
+Suite.prototype.eachTest = function(fn){
+ utils.forEach(this.tests, fn);
+ utils.forEach(this.suites, function(suite){
+ suite.eachTest(fn);
+ });
+ return this;
+};
+
}); // module: suite.js
require.register("test.js", function(module, exports, require){