Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #325 - add better grep support to js api #407

Merged
merged 1 commit into from
May 1, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
})
})
})