☕️ Classic, reliable, trusted test framework for Node.js and the browser
BugFix: Support for correct runner.total count when using --grep
Brendan Neebd1d411fe4bb
diff --git a/bin/_mocha b/bin/_mochaindex 3a7a903d..031bc535 100755--- a/bin/_mocha+++ b/bin/_mocha@@ -269,10 +269,10 @@ function load(files, fn) {function run(suite, fn) {suite.emit('run');var runner = new Runner(suite);- var reporter = new Reporter(runner);runner.globals(program.globals);if (program.ignoreLeaks) runner.ignoreLeaks = true;if (program.grep) runner.grep(new RegExp(program.grep));+ var reporter = new Reporter(runner);if (program.growl) growl(runner, reporter);runner.run(fn);}diff --git a/lib/runner.js b/lib/runner.jsindex c836d6b6..664c6114 100644--- a/lib/runner.js+++ b/lib/runner.js@@ -53,7 +53,8 @@ function Runner(suite) {Runner.prototype.__proto__ = EventEmitter.prototype;/**- * 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@@ -61,9 +62,17 @@ Runner.prototype.__proto__ = EventEmitter.prototype;*/Runner.prototype.grep = function(re){+ var self = this;debug('grep %s', re);- this._grep = re;- return this;+ self._grep = re;++ self.suite.iterateTests(function(test){+ if (!self._grep.test(test.fullTitle())){+ self.total--;+ };+ });++ return self;};/**diff --git a/lib/suite.js b/lib/suite.jsindex 257b502e..b17b2c89 100644--- a/lib/suite.js+++ b/lib/suite.js@@ -245,3 +245,25 @@ Suite.prototype.total = function(){return sum + suite.total();}, 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 public+ */++Suite.prototype.iterateTests = function(fn){+ var self = this;+ self.tests.forEach(function(test){+ fn(test);+ });+ self.suites.forEach(function(suite){+ suite.iterateTests(fn);+ });+ return this;+};+diff --git a/test/runner.js b/test/runner.jsindex 20a0afb8..bc5e28ee 100644--- a/test/runner.js+++ b/test/runner.js@@ -1,7 +1,8 @@var mocha = require('../'), Suite = mocha.Suite- , Runner = mocha.Runner;+ , Runner = mocha.Runner+ , Test = mocha.Test;describe('Runner', function(){var suite, runner;@@ -11,6 +12,17 @@ describe('Runner', function(){runner = new Runner(suite);})+ describe('.grep()', function(){+ it('should update the runner.total with 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'));+ var newRunner = new Runner(suite);+ newRunner.grep(/lions/);+ newRunner.total.should.equal(2);+ })+ })+describe('.globals()', function(){it('should default to the known globals', function(){runner.globals().length.should.be.above(10);@@ -102,4 +114,4 @@ describe('Runner', function(){runner.failHook(hook, err);})})-})\ No newline at end of file+})diff --git a/test/suite.js b/test/suite.jsindex 6d875f4e..2b8865d6 100644--- a/test/suite.js+++ b/test/suite.js@@ -249,4 +249,52 @@ describe('Suite', function(){});});});++ describe('.iterateTests(fn)', function(){+ beforeEach(function(){+ this.suite = new Suite('A Suite');+ });++ describe('when there are no nested suites or tests', function(){+ it('should return 0', function(){+ var counter = 0;+ function fn(){+ counter++;+ }+ this.suite.iterateTests(fn);+ counter.should.equal(0);+ });+ });++ describe('when there are several tests in the suite', function(){+ it('should return the number', function(){+ this.suite.addTest(new Test('a child test'));+ this.suite.addTest(new Test('another child test'));++ var counter = 0;+ function fn(){+ counter++;+ }+ this.suite.iterateTests(fn);+ counter.should.equal(2);+ });+ });++ describe('when there are several levels of nested suites', function(){+ it('should return the number', function(){+ this.suite.addTest(new Test('a child test'));+ var suite = (new Suite('a child suite'));+ suite.addTest(new Test('a test in a child suite'));+ this.suite.addSuite(suite);++ var counter = 0;+ function fn(){+ counter++;+ }+ this.suite.iterateTests(fn);+ counter.should.equal(2);+ });+ });++ });});