Skip to content

Commit

Permalink
Fix issue mochajs#325 - add better grep support to js api
Browse files Browse the repository at this point in the history
  • Loading branch information
davemckenna01 committed Apr 30, 2012
1 parent c9d183c commit 58cd48e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ function image(name) {
* - `globals` array of accepted globals
* - `timeout` timeout in milliseconds
* - `ignoreLeaks` ignore global leaks
* - `grep` string or regexp to filter tests with
*
* @param {Object} options
* @api public
*/

function Mocha(options) {
options = options || {};
options.grep = 'string' == typeof options.grep
? new RegExp(options.grep)
: options.grep;
this.files = [];
this.options = options;
this.suite = new exports.Suite('', new exports.Context);
Expand Down Expand Up @@ -154,6 +158,21 @@ Mocha.prototype.growl = function(runner, reporter) {
});
};

/**
* Add regexp to grep for to the options object
*
* @param {RegExp} or {String} re
* @return {Mocha}
* @api public
*/

Mocha.prototype.grep = function(re){
this.options.grep = 'string' == typeof re
? new RegExp(re)
: re;
return this;
};

/**
* Run tests and invoke `fn()` when complete.
*
Expand Down
44 changes: 44 additions & 0 deletions test/jsapi/grep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var Mocha = require('../../');

describe('Mocha', function(){

beforeEach(function(){
this.reEqual = function(r1, r2){
return r1.source === r2.source
&& r1.global === r2.global
&& r1.ignoreCase === r2.ignoreCase
&& r1.multiline === r2.multiline;
}
})

describe('constructor options.grep', function(){
it('should add a RegExp to the mocha.options object', function(){
var mocha = new Mocha({grep:/foo/});
this.reEqual(/foo/, mocha.options.grep).should.be.ok;
})

it('should convert grep string to a RegExp', function(){
var mocha = new Mocha({grep:'foo'});
this.reEqual(/foo/, mocha.options.grep).should.be.ok;
})
})

describe('.grep()', function(){
it('should add a RegExp to the mocha.options object', function(){
var mocha = new Mocha();
mocha.grep(/foo/);
this.reEqual(/foo/, mocha.options.grep).should.be.ok;
})

it('should convert grep string to a RegExp', function(){
var mocha = new Mocha();
mocha.grep('foo');
this.reEqual(/foo/, mocha.options.grep).should.be.ok;
})

it('should return it\'s parent Mocha object for chainability', function(){
var mocha = new Mocha();
mocha.grep().should.equal(mocha);
})
})
})

0 comments on commit 58cd48e

Please sign in to comment.