Reputation: 1168
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:
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))"
Mocha Command --invert
mocha -g "wanted_string" | mocha -i 1 -g "unwanted_string"
Upvotes: 0
Views: 820
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...
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.
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/,
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