Skip to content

armgit / mocha

Public Imported from GitHub
0

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

feature(mocha/grep): improve grep - issue #808

Ariel Mashraki6e6bd574d929
diff --git a/bin/_mocha b/bin/_mocha
index 8a3f50ad..a6adadd3 100755
--- a/bin/_mocha
+++ b/bin/_mocha
@@ -254,11 +254,11 @@ mocha.suite.bail(program.bail);
// --grep
-if (program.grep) mocha.grep(new RegExp(program.grep));
+if (program.grep) mocha.grep(program.grep);
// --fgrep
-if (program.fgrep) mocha.grep(program.fgrep);
+if (program.fgrep) mocha.fgrep(program.fgrep);
// --invert
diff --git a/browser-entry.js b/browser-entry.js
index 27ea71f5..341a1999 100644
--- a/browser-entry.js
+++ b/browser-entry.js
@@ -132,8 +132,8 @@ mocha.run = function(fn){
mocha.globals('location');
var query = Mocha.utils.parseQuery(global.location.search || '');
- if (query.grep) mocha.grep(new RegExp(query.grep));
- if (query.fgrep) mocha.grep(query.fgrep);
+ if (query.grep) mocha.grep(query.grep);
+ if (query.fgrep) mocha.fgrep(query.fgrep);
if (query.invert) mocha.invert();
return Mocha.prototype.run.call(mocha, function(err){
diff --git a/lib/mocha.js b/lib/mocha.js
index 46775ab5..de0f32ee 100644
--- a/lib/mocha.js
+++ b/lib/mocha.js
@@ -80,7 +80,7 @@ function Mocha(options) {
this.grep(new RegExp(options.grep));
}
if (options.fgrep) {
- this.grep(options.fgrep);
+ this.fgrep(options.fgrep);
}
this.suite = new exports.Suite('', new exports.Context());
this.ui(options.ui);
@@ -246,6 +246,17 @@ Mocha.prototype._growl = function(runner, reporter) {
});
};
+/**
+ * Escape string and add it to grep as a regexp.
+ *
+ * @api public
+ * @param str
+ * @returns {Mocha}
+ */
+Mocha.prototype.fgrep = function(str) {
+ return this.grep(new RegExp(escapeRe(str)));
+};
+
/**
* Add regexp to grep, if `re` is a string it is escaped.
*
@@ -256,10 +267,15 @@ Mocha.prototype._growl = function(runner, reporter) {
* @return {Mocha}
*/
Mocha.prototype.grep = function(re) {
- this.options.grep = typeof re === 'string' ? new RegExp(escapeRe(re)) : re;
+ if (utils.isString(re)) {
+ // extract args if it's regex-like, i.e: [string, pattern, flag]
+ var arg = re.match(/^\/(.*)\/(g|i|)$|.*/);
+ this.options.grep = new RegExp(arg[1] || arg[0], arg[2]);
+ } else {
+ this.options.grep = re;
+ }
return this;
};
-
/**
* Invert `.grep()` matches.
*
diff --git a/test/grep.js b/test/grep.js
index ff9d25a1..bbc5c55e 100644
--- a/test/grep.js
+++ b/test/grep.js
@@ -5,44 +5,61 @@ describe('Mocha', function(){
it('should add a RegExp to the mocha.options object', function(){
var mocha = new Mocha({ grep: /foo.*/ });
mocha.options.grep.toString().should.equal('/foo.*/');
- })
+ });
it('should convert string to a RegExp', function(){
var mocha = new Mocha({ grep: 'foo.*' });
mocha.options.grep.toString().should.equal('/foo.*/');
- })
- })
+ });
+ });
describe('"fgrep" option', function(){
it('should escape and convert string to a RegExp', function(){
var mocha = new Mocha({ fgrep: 'foo.*' });
mocha.options.grep.toString().should.equal('/foo\\.\\*/');
- })
- })
+ });
+ });
- describe('.grep()', function(){
- it('should add a RegExp to the mocha.options object', function(){
- var mocha = new Mocha;
- mocha.grep(/foo/);
- mocha.options.grep.toString().should.equal('/foo/');
- })
+ describe('.grep()', function() {
+ // Test helper
+ function testGrep(mocha) {
+ return function testGrep(grep, expected) {
+ mocha.grep(grep);
+ mocha.options.grep.toString().should.equal(expected);
+ }
+ }
- it('should convert grep string to a RegExp', function(){
- var mocha = new Mocha;
- mocha.grep('foo');
- mocha.options.grep.toString().should.equal('/foo/');
- })
+ it('should add a RegExp to the mocha.options object', function() {
+ var test = testGrep(new Mocha);
+ test(/foo/, '/foo/');
+ });
+
+ it('should convert grep string to a RegExp', function() {
+ var test = testGrep(new Mocha);
+ test('foo', '/foo/');
+ test('^foo.*bar$', '/^foo.*bar$/');
+ test('^@.*(?=\\(\\)$)', '/^@.*(?=\\(\\)$)/');
+ });
+
+ it('should covert grep regex-like string to a RegExp', function() {
+ var test = testGrep(new Mocha);
+ test('/foo/', '/foo/');
+ // Keep the flags
+ test('/baz/i', '/baz/i');
+ test('/bar/g', '/bar/g');
+ test('/^foo(.*)bar/g', '/^foo(.*)bar/g');
+ });
it('should return it\'s parent Mocha object for chainability', function(){
var mocha = new Mocha;
mocha.grep().should.equal(mocha);
- })
- })
+ });
+ });
describe('"invert" option', function(){
it('should add a Boolean to the mocha.options object', function(){
var mocha = new Mocha({ invert: true });
- mocha.options.invert.should.be.ok();
- })
- })
-})
+ mocha.options.invert.should.be.ok;
+ });
+ });
+});
diff --git a/test/integration/fixtures/options/grep.js b/test/integration/fixtures/options/grep.js
index 5e163f5e..94785dc7 100644
--- a/test/integration/fixtures/options/grep.js
+++ b/test/integration/fixtures/options/grep.js
@@ -1,10 +1,15 @@
describe('grep', function() {
+ describe('Match', function() {
+ it('should run', function(){});
+ it('should also run', function() {});
+ });
+
describe('match', function() {
it('should run', function(){});
it('should also run', function() {});
});
- describe('fail', function(){
+ describe('fail', function() {
it('should not be ran', function() {
throw new Error('Spec should not run');
});
diff --git a/test/integration/options.js b/test/integration/options.js
index 1e25b6ef..afd961b3 100644
--- a/test/integration/options.js
+++ b/test/integration/options.js
@@ -125,15 +125,29 @@ describe('options', function() {
});
});
- it('runs specs matching a RegExp', function(done) {
- args = ['--grep', '.*'];
- run('options/grep.js', args, function(err, res) {
- assert(!err);
- assert.equal(res.stats.pending, 0);
- assert.equal(res.stats.passes, 2);
- assert.equal(res.stats.failures, 1);
- assert.equal(res.code, 1);
- done();
+ describe('runs specs matching a RegExp', function() {
+ it('with RegExp like strings(pattern follow by flag)', function(done) {
+ args = ['--grep', '/match/i'];
+ run('options/grep.js', args, function(err, res) {
+ assert(!err);
+ assert.equal(res.stats.pending, 0);
+ assert.equal(res.stats.passes, 4);
+ assert.equal(res.stats.failures, 0);
+ assert.equal(res.code, 0);
+ done();
+ });
+ });
+
+ it('string as pattern', function(done) {
+ args = ['--grep', '.*'];
+ run('options/grep.js', args, function(err, res) {
+ assert(!err);
+ assert.equal(res.stats.pending, 0);
+ assert.equal(res.stats.passes, 4);
+ assert.equal(res.stats.failures, 1);
+ assert.equal(res.code, 1);
+ done();
+ });
});
});
@@ -143,7 +157,7 @@ describe('options', function() {
run('options/grep.js', args, function(err, res) {
assert(!err);
assert.equal(res.stats.pending, 0);
- assert.equal(res.stats.passes, 2);
+ assert.equal(res.stats.passes, 4);
assert.equal(res.stats.failures, 0);
assert.equal(res.code, 0);
done();