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

Docs: grep() using RegExp with global flag /g #4714

Merged
merged 1 commit into from
Aug 14, 2021
Merged

Docs: grep() using RegExp with global flag /g #4714

merged 1 commit into from
Aug 14, 2021

Conversation

juergba
Copy link
Member

@juergba juergba commented Aug 11, 2021

Description

The global flag /g in a RegExp expression is behaving weirdly with Mocha's --grep option. The selection of tests based on the fullTitle seems wrong as tests get lost, but at the end it's just javascript.

let reIgnore = /a/g;

console.log(reIgnore.test("a")); // true
console.log(reIgnore.test("a")); // false
console.log(reIgnore.test("a")); // true
console.log(reIgnore.test("a")); // false

Description of the Change

Using a flag as /g or /y doesn't make any sense in this context.

  • improve Mocha.prototype.grep for better conversion of string into RegExp
  • clarify documentation

Alternate Designs

Instead of re-using the same RegExp instance for checking each test, we could construct new instances:

let reIgnore = /a/g;

console.log(new RegExp(reIgnore).test("a")); // true
console.log(new RegExp(reIgnore).test("a")); // true
console.log(new RegExp(reIgnore).test("a")); // true
console.log(new RegExp(reIgnore).test("a")); // true

Mocha's grep selection is running in any case, with the --grep value or with our defaultGrep /.*/. Therefore I decided to not apply any fix for this edge case.

Applicable issues

closes #4704

@juergba juergba self-assigned this Aug 11, 2021
@juergba juergba added type: bug a defect, confirmed by a maintainer semver-patch implementation requires increase of "patch" version number; "bug fixes" labels Aug 11, 2021
@juergba juergba requested a review from a team August 11, 2021 09:11
@juergba juergba added the area: documentation anything involving docs or mochajs.org label Aug 11, 2021
@@ -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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<br> is not needed here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried, there is no end-of-line without <br> in our markdown file.

@juergba juergba merged commit 757b85d into master Aug 14, 2021
@juergba juergba deleted the juergba/grep branch August 14, 2021 16:26
@juergba juergba added this to the next milestone Aug 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: documentation anything involving docs or mochajs.org semver-patch implementation requires increase of "patch" version number; "bug fixes" type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Grep function randomly skips tests
2 participants