nyc npm testa code coverage tool built on istanbul that works for applications that spawn subprocesses.
Simply run your tests with nyc, and it will collect coverage information for
each process and store it in .nyc_output.
nyc npm testyou can pass a list of Istanbul reporters that you'd like to run:
nyc --reporter=lcov --reporter=text-lcov npm testIf you're so inclined, you can simply add nyc to the test stanza in your package.json:
{
"script": {
"test": "nyc tap ./test/*.js"
}
}nyc supports custom require hooks like
babel-register. If necessary nyc can
load the hooks for you, using the --require
flag.
Source maps are used to map coverage information back to the appropriate lines
of the pre-transpiled code. You'll have to configure your custom require hook
to inline the source map in the transpiled code. For Babel that means setting
the sourceMaps option to inline.
nyc exposes istanbul's check-coverage tool. After running your tests with nyc, simply run:
nyc check-coverage --lines 95 --functions 95 --branches 95This feature makes it easy to fail your tests if coverage drops below a given threshold.
Once you've run your tests with nyc, simply run:
nyc reportTo view your coverage report:
--------------------|-----------|-----------|-----------|-----------|
File | % Stmts |% Branches | % Funcs | % Lines |
--------------------|-----------|-----------|-----------|-----------|
./ | 85.96 | 50 | 75 | 92.31 |
index.js | 85.96 | 50 | 75 | 92.31 |
./test/ | 98.08 | 50 | 95 | 98.04 |
nyc-test.js | 98.08 | 50 | 95 | 98.04 |
./test/fixtures/ | 100 | 100 | 100 | 100 |
sigint.js | 100 | 100 | 100 | 100 |
sigterm.js | 100 | 100 | 100 | 100 |
--------------------|-----------|-----------|-----------|-----------|
All files | 91.89 | 50 | 86.11 | 95.24 |
--------------------|-----------|-----------|-----------|-----------|you can use any reporters that are supported by istanbul:
nyc report --reporter=lcovBy default nyc does not instrument files in node_modules, or test
for coverage. You can override this setting in your package.json, by
adding the following configuration:
{"config": {
"nyc": {
"exclude": [
"node_modules/"
]
}
}}By default nyc does not collect coverage for files that have not
been required, run nyc with the flag --all to enable this.
The --require flag can be provided to nyc to indicate that additional
modules should be required in the subprocess collecting coverage:
nyc --require babel-core/register --require babel-polyfill mocha
Behind the scenes nyc uses istanbul. You
can place a .istanbul.yml file in your project's root directory to pass config
setings to istanbul's code instrumenter:
instrumentation:
preserve-comments: truecoveralls.io is a great tool for adding coverage reports to your GitHub project. Here's how to get nyc integrated with coveralls and travis-ci.org:
- add the coveralls and nyc dependencies to your module:
npm install coveralls nyc --save- update the scripts in your package.json to include these bins:
{
"script": {
"test": "nyc tap ./test/*.js",
"coverage": "nyc report --reporter=text-lcov | coveralls",
}
}-
add the environment variable
COVERALLS_REPO_TOKENto travis, this is used by the coveralls bin. -
add the following to your
.travis.yml:
after_success: npm run coverageThat's all there is to it!
Note: by default coveralls.io adds comments to pull-requests on GitHub, this can
feel intrusive. To disable this, click on your repo on coveralls.io and uncheck LEAVE COMMENTS?.
