Tamlyn
Tamlyn

Reputation: 23562

Keep Mocha tests alongside source files

I currently have my NodeJS source files in src and test suites in test, e.g.:

/src/bar/baz/foo.js
/test/bar/baz/foo.spec.js

This leads to awkward require statements like var foo = require('../../../src/bar/baz/foo'). And it's hard to see at a glance which source files are missing tests. I would like to instead keep my test suites in the same directory as the relevant source files:

/src/bar/baz/foo.js
/src/bar/baz/foo.spec.js

But now running mocha --recursive src causes errors as Mocha tries to run my source files as tests.

I've seen suggestions of using find or gulp to filter the file list but I find it surprising that this can't be done with plain Mocha. What's the recommended way of organising files this way?

Upvotes: 12

Views: 2264

Answers (1)

Rodrigo Medeiros
Rodrigo Medeiros

Reputation: 7862

Just pass the pattern of your test files to mocha, like:

mocha "src/**/*.spec.js"

This is going to run the .spec.js files in all subdirectories of src.

Upvotes: 17

Related Questions