Mocha & Typescript and recursively loading

Viewed 1103

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

However, the users.spec.ts test file doesn't get loaded but the authentications.spec.ts does.

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?

3 Answers

It looks like you also asked this question here, and got a good answer: quote the glob argument, so that it's expanded (recursively) by mocha, rather than by the shell (non-recursively):

"test": "mocha --require ts-node/register 'test/**/*.spec.ts' --opts ./test/mocha.opts"

It certainly seems that there is a bug here, whether in mocha's handling of --recursive or some interaction with ts-node I don't know.

You can work around this by telling mocha to specifically look for more deeply nested folders by using a command like:

mocha --recursive --require ts-node/register ./test/**/*.spec.ts ./test/**/**/*.spec.ts --opts ./test/mocha.opts

eg., tell it to specifically look in both the child (/**/) and grandchild (/**/**/) directories.

For me the problem was actually that I had an only in the tests

Related