Skip to content

armgit / mocha

Public Imported from GitHub
0

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

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

Dave McKenna58cd48e14b18
diff --git a/lib/mocha.js b/lib/mocha.js
index d8f9493f..0b1cc83f 100644
--- a/lib/mocha.js
+++ b/lib/mocha.js
@@ -59,6 +59,7 @@ 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
@@ -66,6 +67,9 @@ function image(name) {
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);
@@ -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.
*
diff --git a/test/jsapi/grep.js b/test/jsapi/grep.js
new file mode 100644
index 00000000..f93cb909
--- /dev/null
+++ b/test/jsapi/grep.js
@@ -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);
+ })
+ })
+})