Skip to content

Commit

Permalink
docs: improve 'grep()' and clarify docs (#4714)
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba committed Aug 14, 2021
1 parent f19d3ca commit 757b85d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
7 changes: 5 additions & 2 deletions docs/index.md
Expand Up @@ -1138,17 +1138,20 @@ Cause Mocha to only run tests matching the given `regexp`, which is internally c

Suppose, for example, you have "api" related tests, as well as "app" related tests, as shown in the following snippet; One could use `--grep api` or `--grep app` to run one or the other. The same goes for any other part of a suite or test-case title, `--grep users` would be valid as well, or even `--grep GET`.

And another option with double quotes: `--grep "groupA|groupB"`.<br>
And for more complex criterias: `--grep "/get/i"`. Some shells as e.g. Git-Bash-for-Windows may require: `--grep "'/get/i'"`. Using flags other than the `ignoreCase /i` (especially `/g` and `/y`) may lead to unpredictable results.

```js
describe('api', function() {
describe('GET /api/users', function() {
describe('GET /api/users groupA', function() {
it('respond with an array of users', function() {
// ...
});
});
});

describe('app', function() {
describe('GET /users', function() {
describe('GET /users groupB', function() {
it('respond with an array of users', function() {
// ...
});
Expand Down
3 changes: 2 additions & 1 deletion example/config/.mocharc.js
@@ -1,7 +1,8 @@
'use strict';

// This is a JavaScript-based config file containing every Mocha option plus others.
// If you need conditional logic, you might want to use this type of config.
// If you need conditional logic, you might want to use this type of config,
// e.g. set options via environment variables 'process.env'.
// Otherwise, JSON or YAML is recommended.

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion lib/mocha.js
Expand Up @@ -581,7 +581,7 @@ Mocha.prototype.fgrep = function(str) {
Mocha.prototype.grep = function(re) {
if (utils.isString(re)) {
// extract args if it's regex-like, i.e: [string, pattern, flag]
var arg = re.match(/^\/(.*)\/(g|i|)$|.*/);
var arg = re.match(/^\/(.*)\/([gimy]{0,4})$|.*/);
this.options.grep = new RegExp(arg[1] || arg[0], arg[2]);
} else {
this.options.grep = re;
Expand Down

0 comments on commit 757b85d

Please sign in to comment.