mocha, ts-node and recursively loading files

Created on 23 Nov 2017  Â·  1Comment  Â·  Source: mochajs/mocha

Using this script:

"test": "./node_modules/mocha/bin/mocha --recursive --require ts-node/register ./test/**/*.spec.ts --opts ./test/mocha.opts",

I've tried to load some test files inside the tree:

.
├── controllers
│   ├── admin
│   │   └── users.spec.ts
│   ├── authentications.spec.ts

The authentications.spec.ts test file gets loaded but the users.spec.ts doesn't.

My mocha.opts file looks like just:

--timeout 5000

I've also tried to remove the --recursive flag and just have a glob but it doesn't work.

I can't work out whether the problem is with ts-node or with mocha?

Any ideas?

question

Most helpful comment

Quote the glob, "./test/**/*.spec.ts" instead of ./test/**/*.spec.ts, so the shell doesn't interpret the **. (Most shells treat that as including any number of directories, but it's technically an extension and isn't enabled in all of them by default; quoting forces the original glob to be passed to Mocha, which interprets it the same no matter which OS or shell it's inside.) If for some reason that doesn't resolve your issue, please let us know!

Tangentially, --recursive doesn't typically affect globs one way or the other; at least, it doesn't affect glob interpretation (* is never recursive and ** in Mocha is always recursive), and won't kick in when the glob only matches files (I'm not certain whether it comes into play when a glob matches a directory; I haven't ever had a reason to try). It changes whether Mocha looks for JS files inside subdirectories or only that exact directory when a directory is specified as the filepath. (So, for instance, if your tests are all JS files somewhere under the default test folder, you can put --recursive in mocha.opts to make Mocha run all the tests by default without having specified a path/glob, then specify a path/glob when running it to effectively override that and run only some files. In fact, I'm not sure what other good use cases it has.)

(Also, ./test/mocha.opts is the default opts file location, so you should be able to omit the --opts parameter when it's at that path.)

>All comments

Quote the glob, "./test/**/*.spec.ts" instead of ./test/**/*.spec.ts, so the shell doesn't interpret the **. (Most shells treat that as including any number of directories, but it's technically an extension and isn't enabled in all of them by default; quoting forces the original glob to be passed to Mocha, which interprets it the same no matter which OS or shell it's inside.) If for some reason that doesn't resolve your issue, please let us know!

Tangentially, --recursive doesn't typically affect globs one way or the other; at least, it doesn't affect glob interpretation (* is never recursive and ** in Mocha is always recursive), and won't kick in when the glob only matches files (I'm not certain whether it comes into play when a glob matches a directory; I haven't ever had a reason to try). It changes whether Mocha looks for JS files inside subdirectories or only that exact directory when a directory is specified as the filepath. (So, for instance, if your tests are all JS files somewhere under the default test folder, you can put --recursive in mocha.opts to make Mocha run all the tests by default without having specified a path/glob, then specify a path/glob when running it to effectively override that and run only some files. In fact, I'm not sure what other good use cases it has.)

(Also, ./test/mocha.opts is the default opts file location, so you should be able to omit the --opts parameter when it's at that path.)

Was this page helpful?
0 / 5 - 0 ratings