- 1 year ago 68
Node.js Question

How set up test code coverage with different test levels?

I'm using nyc for generating Code coverage reports. I use different levels for my tests. How I can merge reports from different levels?

There is my part of my package.json

"scripts": {
"test": "npm run test:unit && npm run test:component",
"test:component": "nyc mocha ./test/component",
"test:unit": "nyc mocha ./test/unit"
"nyc": {
"extension": [
"cache": true,
"reporter": [

Answer Source

You can use nyc and mocha in a sequence to achieve this effect.

With npm scripts this will look like :

    "scripts": {
        "coverage" : "nyc npm run test",
        "test": "npm run test:unit && npm run test:component",
        "test:component": "mocha ./test/component",
        "test:unit": "mocha ./test/unit"
    "nyc": { ... }

The main idea, behind nyc is that it takes all the source files defined in your configuration and instruments them.

Then it runs the command after the instrumentation with modified require, so every command that you run from inside nyc will have the instrumented files as it's source.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download