Search code examples
node.jspattern-matchingmocha.jsglobnode-glob

Glob pattern include all js exclude certain endings


I'd like to use glob with mocha. Sadly mocha does not support exclude and it does not pass glob options either, so I have to solve this in a single pattern without the usage of the glob ignore option. https://github.com/mochajs/mocha/issues/1577 The other solution would be to use mocha.opts, but that is not karma compatible, I am not sure why. I could define a karma compatible opts file for karma and a node compatible opts file for node, but this will break immediately after the issue is fixed, so I am looking for another solution. https://github.com/karma-runner/karma-mocha/issues/88

I need to match test/**/*.spec.js and exclude test/**/*.karma.spec.js from the results. Is this possible with a single glob pattern?


Solution

  • What we are talking here is the AND("*.spec.js", NOT("*.karma.spec.js")) expressed with logical operators. I checked the glob documentation, it does not support the AND logical operator.

    Lucky for us we can transform this operator to OR with negation: NOT(OR(NOT("*.spec.js"), "*.karma.spec.js")). I tried out the test/**/!((!(*.spec.js)|*.karma.spec.js)), but it does not work. It tries to require a module with the test/features path, which is a path of a directory and not a js file, so no wonder it does not manage it. I don't know what kind of bug is that, but it is apparently not working.

    Another possible solution to forget this AND operator, and use only negation with NOT("*.karma").spec.js. I tried this out with test/**/!(*.karma).spec.js, which was working properly.