1

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?

inf3rno
  • 24,976
  • 11
  • 115
  • 197
  • I tried with `"test": "mocha --reporter spec \"test/**/*(.spec.js|!(.node.spec.js))\"",` so far. It is parsed, but I got an error that cannot find module `test/features`. This is odd, because there are no `.spec.js` or node modules in that folder, only `.feature` files for BDD. – inf3rno Mar 16 '16 at 01:05

2 Answers2

2

You could always use mocha and find together. For example, the following command will return a list of test/**/*.spec.js and exclude test/**/*.karma.spec.js:

find test -name '*.spec.js' ! -path '*.karma.spec.js'

Combine it with mocha to achieve what you want:

mocha $(find test -name '*.spec.js' ! -path '*.karma.spec.js')
Gabriel Florit
  • 2,886
  • 1
  • 22
  • 36
1

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.

inf3rno
  • 24,976
  • 11
  • 115
  • 197