Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 513
Tamlyn Avatar asked Jun 29 '15 14:06

Tamlyn


1 Answers

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.

like image 151
Rodrigo Medeiros Avatar answered Oct 12 '22 07:10

Rodrigo Medeiros