Search code examples
javascriptunit-testingmocha.jsnyc

How to filter out certain patterns from mocha/nyc test coverage (Node.js)?


In mocha.opts, I am sure how to set-up coverage pattern that needs to be taken into account but not sure how to include the patterns that has to be filtered-out?

For example,

I have the file mocha.opts containing coverage patterns which is passed as param to the following command:
nyc mocha --opts ./mocha.opts

The content of the mocha.opts is given below:
test/tests/routes/*.test.js

But there are lot of custom js scripts imported into *.test.js files. But these custom js scripts contains functions which I don't want to be covered under coverage report nor I have written unit test for them.
Is there any way to filter out these patterns from coverage by declaring them into mocha.opts file?


Solution

  • In package.json, you can add nyc configuration. Something like this. https://www.npmjs.com/package/nyc#excluding-files

    "nyc": {
      "include": [
        "./**/*.js"
      ],
      "exclude": [
        "./test/",
        "./db/migrations/"
      ]
    }
    

    I haven't seen a way to specify in mocha.opts though.