Reputation: 6085
I'm integrating tests into my Node server and am having some trouble with unit testing. When I run npm test
in my root directory, Mocha automatically runs all tests inside of my test
folder. However, the unit tests which are dispersed throughout the project do not run. How can I ensure that Mocha automatically runs them?
Upvotes: 4
Views: 2476
Reputation: 42594
I don't see why you would want to have your tests "dispersed throughout the project". I think you would be much better off keeping all of them in a dedicated test folder. You can mirror the folder structure of your src
folder, if your concern is that you want to easily find the test for a specific source file. Example:
project/
├── src/
│ ├── one/
│ │ └── foo.js
│ └── two/
│ └── bar.js
└── test/
├── one/
│ └── foo.test.js
└── two/
└── bar.test.js
Regardless of whether you decide to reorganise your project, the way to get mocha to execute all test files recursively is with the following:
scripts: {
"test": "mocha \"./**/*.test.js\""
}
You can replace the .
with test
or src
(or any other folder) if you don't want to search the whole project folder.
You can read more about it here: How do I get mocha to execute tests in all subfolders recursively?
Upvotes: 3
Reputation: 5303
You could modify the npm test
command to find
your test files and run mocha
against them.
Assuming a directory structure like:
project/
src/
test/
main-tests.test.js
things/
test/
thing.test.js
more_things/
more-things.test.js
You would change your package.json
like this:
scripts: {
...
"test": "mocha $(find . -name '*.test.js')"
...
}
There are probably other ways to do this, like specifying the expected glob patterns of the locations of your test files.
Both of these methods require a plain pattern to the names of your test files.
Upvotes: 7