Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Mocha to execute unit tests in multiple subfolders in Node.js

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?

like image 487
Patrick Connors Avatar asked Sep 20 '25 07:09

Patrick Connors


1 Answers

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.

like image 52
Matt Morgan Avatar answered Sep 21 '25 23:09

Matt Morgan