Lusha Li
Lusha Li

Reputation: 1168

How to run the mocha test that their describe only includes a string and excludes another string?

The following line works if I want to run tests including a string wanted_string:

mocha -g "wanted_string"

Then on the top of that, how could I exclude a string unwanted_string. The mocha command -g [https://mochajs.org/#command-line-usage] allows regex, but I am not able to figure it out.



Something I have tried:

  1. Different Regex

    mocha -g "(?=.*wanted_string)(?!.*unwanted_string).*" 
    

from Create Regex to find whole word only if includes one string and excludes another

mocha -g "^(?=.*(?:wanted_string))(?:unwanted_string))"
  1. Mocha Command --invert

    mocha -g "wanted_string" | mocha -i 1 -g "unwanted_string"
    

Upvotes: 0

Views: 820

Answers (2)

Lusha Li
Lusha Li

Reputation: 1168

mocha --grep "^(?=.*(?:wanted))((?! unwanted).)*$"

Upvotes: 0

Inigo
Inigo

Reputation: 15030

A Regex to to do that is extremely hard if not impossible (I'm pretty sure JS RegEx is not Turing Complete). The example you gave works because the test only happens at the beginning of the string.

Unless I am wrong, here are you options...

Rely on something else to select tests

Instead of relying on filtering by combinations of include and exclude, you could organize tests in a directory hierarchy. Then you can select tests in a specific branch of the hierarchy and use Regex to exclude some of those.

Use Mocha's test skipping functionality

If the ones you want to exclude are few, it takes little time to temporarily modify the test function or suite so it doesn't run: https://danielkorn.io/post/skipping-tests-in-mochajs/,

Write your own Mocha Root Hook

You could write a Mocha Root Hook that took separate include and exclude patterns from a config file or environment variable and the skip tests based on that.

Upvotes: 1

Related Questions