☕️ Classic, reliable, trusted test framework for Node.js and the browser
Use grep as regexp (close #1381)
Ariel Mashraki83bd5b4b1d95
diff --git a/lib/runner.js b/lib/runner.jsindex ba02d9a5..0c9cc551 100644--- a/lib/runner.js+++ b/lib/runner.js@@ -574,8 +574,8 @@ Runner.prototype.uncaught = function(err){*/Runner.prototype.run = function(fn){- var self = this- , fn = fn || function(){};+ var self = this;+ fn = fn || function(){};function uncaught(err){self.uncaught(err);diff --git a/mocha.js b/mocha.jsindex 22d1a9f3..6352ac70 100644--- a/mocha.js+++ b/mocha.js@@ -5092,8 +5092,8 @@ Runner.prototype.uncaught = function(err){*/Runner.prototype.run = function(fn){- var self = this- , fn = fn || function(){};+ var self = this;+ fn = fn || function(){};function uncaught(err){self.uncaught(err);@@ -5968,7 +5968,7 @@ exports.stringify = function(value) {}for (prop in value) {- if (value.hasOwnProperty(prop)) {+ if (Object.prototype.hasOwnProperty.call(value, prop)) {return JSON.stringify(exports.canonicalize(value), null, 2).replace(/,(\n|$)/g, '$1');}}@@ -6277,7 +6277,7 @@ mocha.run = function(fn){mocha.globals('location');var query = Mocha.utils.parseQuery(global.location.search || '');- if (query.grep) mocha.grep(query.grep);+ if (query.grep) mocha.grep(new RegExp(query.grep));if (query.invert) mocha.invert();return Mocha.prototype.run.call(mocha, function(err){diff --git a/support/tail.js b/support/tail.jsindex e9c0c0d2..5e1d59be 100644--- a/support/tail.js+++ b/support/tail.js@@ -139,7 +139,7 @@ mocha.run = function(fn){mocha.globals('location');var query = Mocha.utils.parseQuery(global.location.search || '');- if (query.grep) mocha.grep(query.grep);+ if (query.grep) mocha.grep(new RegExp(query.grep));if (query.invert) mocha.invert();return Mocha.prototype.run.call(mocha, function(err){diff --git a/test/browser/grep.html b/test/browser/grep.htmlnew file mode 100644index 00000000..0ba47c85--- /dev/null+++ b/test/browser/grep.html@@ -0,0 +1,51 @@+<html>+ <head>+ <title>Mocha</title>+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">+ <meta name="viewport" content="width=device-width, initial-scale=1.0">+ <link rel="stylesheet" href="../../mocha.css" />+ <script src="../../mocha.js"></script>+ <script>mocha.setup('bdd')</script>+ <script>+ function assert(expr, msg) {+ if (!expr) throw new Error(msg || 'failed');+ }+ </script>+ <script src="grep.js"></script>+ </head>+ <body>+ <div id="mocha"></div>+ <script>+ (function(window) {+ var location = window.location;+ mocha.checkLeaks();+ var runner = mocha.run();+ setTimeout(run, 1000);++ function run() {+ var regex = [+ '.*', // All+ 'm{2}', // 'mm...m'+ '\\d', // Contains number+ '^\\d{2}(?=\\s\\d$)', // Start with 2 numbers and end with one+ '^@.*(?=\\(\\)$)', // Run @Array and @Function suite, but only function+ '^@(?!.*\\)$)', // Run @Array and @Function suite, but only properties+ '^co', // Start with 'co'+ 'first$', // Ends with 'first'+ '^co.*(?=second$)', // Starts with 'co', ends with 'second'+ '^Date:\\s01\/0(?:[1-4])\/2015$', // Run all tests between '01/[01-04]/2015'+ encodeURIComponent('^#'), // Run encoded => start with '#'+ encodeURIComponent('^[^a-z|0-9]+$') // Run encoded => only uppercase suites(include `it` fns), e.g: CONSTANTS+ ]+ , qs = location.search.replace('?grep=', '')+ , re = ~qs.indexOf('%') ? qs : decodeURIComponent(qs)+ , grep = regex[regex.indexOf(re) + 1];++ return grep+ ? location.search = 'grep=' + grep+ : false;+ }+ })(window);+ </script>+ </body>+</html>diff --git a/test/browser/grep.js b/test/browser/grep.jsnew file mode 100644index 00000000..9531b87a--- /dev/null+++ b/test/browser/grep.js@@ -0,0 +1,108 @@+// numbers+describe('21', function() {+ it('1', function() {+ assert(true);+ });+ it('2', function() {+ assert(true);+ });+});+// symbols+describe('@Array', function() {+ it('.pop()', function() {+ assert(true);+ });+ it('.push()', function() {+ assert(true);+ });+ it('.length', function() {+ assert(true);+ });+});++describe('@Function', function() {+ it('.call()', function() {+ assert(true);+ });+ it('.apply()', function() {+ assert(true);+ });+ it('.length', function() {+ assert(true);+ });+ it('.name', function() {+ assert(true);+ });+ it('.prototype', function() {+ assert(true);+ });+});++//url with hashtags+describe('#Services',function() {+ describe('#http', function() {+ it('.createClient()', function() {+ assert(true);+ });+ it('.Server()', function() {+ assert(true);+ });+ });+ describe('#crypto', function() {+ it('.randomBytes()', function() {+ assert(true);+ });+ it('.Hmac()', function() {+ assert(true);+ });+ });+});++// Uppercase+describe('CONSTANTS', function() {+ it('.STATUS_CODES', function() {+ assert(true);+ });+});++// Dates+describe('Date:', function() {+ it('01/02/2015', function() {+ assert(true);+ });+ it('01/03/2015', function() {+ assert(true);+ });+ it('01/06/2015', function() {+ assert(true);+ });+});++// etc..+describe('booking/summary', function() {+ it('should be run last', function() {+ assert(true);+ });+});++describe('component/booking/summary', function() {+ it('should be run second', function() {+ assert(true);+ });+});++describe('component/booking/intro', function() {+ it('should be run first', function() {+ assert(true);+ });+});++describe('contains numbers', function() {+ it('should run if the number 92 matching', function() {+ assert(true);+ });++ it('should run if the number 8 matching', function() {+ assert(true);+ });+});\ No newline at end of file