Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Fixes #166 - When grepping don't display the empty suites

Xavier Antoviaquecd186e07983d
diff --git a/lib/runner.js b/lib/runner.js
index 664c6114..09e202a6 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -62,17 +62,33 @@ Runner.prototype.__proto__ = EventEmitter.prototype;
*/
Runner.prototype.grep = function(re){
- var self = this;
debug('grep %s', re);
- self._grep = 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
+ */
- self.suite.iterateTests(function(test){
- if (!self._grep.test(test.fullTitle())){
- self.total--;
+Runner.prototype.grepTotal = function(suite) {
+ var self = this;
+ var total = 0;
+
+ suite.iterateTests(function(test){
+ if (self._grep.test(test.fullTitle())){
+ total++;
};
});
- return self;
+ return total;
};
/**
@@ -355,7 +371,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++];
diff --git a/test/runner.js b/test/runner.js
index bc5e28ee..8fdcc92e 100644
--- a/test/runner.js
+++ b/test/runner.js
@@ -23,6 +23,16 @@ describe('Runner', function(){
})
})
+ describe('.grepTotal()', function(){
+ it('should return the total number of matched tests', function(){
+ suite.addTest(new Test('im a test about lions'));
+ suite.addTest(new Test('im another test about lions'));
+ suite.addTest(new Test('im a test about bears'));
+ runner.grep(/lions/);
+ runner.grepTotal(suite).should.equal(2);
+ })
+ })
+
describe('.globals()', function(){
it('should default to the known globals', function(){
runner.globals().length.should.be.above(10);