Search code examples
javascriptmocha.jsistanbul

Running Mocha and Istanbul on multiple cwd


I'm looking for a way to manage testing and getting the coverage of a project that has multiple microservices, which have their own tests in their directories.

└── services
    ├── service-1
    │   ├── src
    │   └── test
    ├── service-2
    │   ├── src
    │   └── test
    └── service-3
        ├── src
        └── test

Each service has its own tests, they all need to be executed from their own cwd in order to make the requires and paths working.

What would be the best way to launch those tests from the root of the project and getting nyc to get the coverage of the whole project ?

As of right now, I'm using a bash script test.sh:

#!/bin/bash
set +e

(cd ./services/service-1 && npm run test)
(cd ./services/service-2 && npm run test)
(cd ./services/service-3 && npm run test)

And launching it with npx nyc ./test.sh, but if a test fail on service-1, service-2 and service-3 will still be executed and considered successful despite the first service failing.


Solution

  • If you executes the tests a single chain of commands, you script exit code will map the first failed test (or the last successfull one if no tests fails)

    #!/bin/bash
    
    cd ./services/service-1 && npm run test \ 
      && cd ../service-2 && npm run test \ 
      && cd ../service-3 && npm run test