kpdecker-nyc v8.4.0
nyc
Istanbul's state of the art command line interface, with support for:
- applications that spawn subprocesses.
- ES2015 transforms, via babel-plugin-istanbul, or source-maps.
Instrumenting your code
You can install nyc as a development dependency and add it to the test stanza in your package.json.
npm i nyc --save-dev{
"script": {
"test": "nyc tap ./test/*.js"
}
}Alternatively, you can install nyc globally and use it to execute npm test:
npm i nyc -gnyc npm testnyc accepts a wide variety of configuration arguments, run nyc --help for
thorough documentation.
Configuration arguments should be provided prior to the program that nyc
is executing. As an example, the following command executes npm test,
and indicates to nyc that it should output both an lcov
and a text-lcov coverage report.
nyc --reporter=lcov --reporter=text-lcov npm testSupport for custom require hooks (babel, webpack, etc.)
nyc supports custom require hooks like
babel-register. 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.
Use with babel-plugin-istanbul for ES6/ES7/ES2015 Support
babel-plugin-istanbul can be used to enable better first-class ES6 support.
enable the
babel-plugin-istanbulplugin:{ "babel": { "presets": ["es2015"], "env": { "test": { "plugins": ["istanbul"] } } } }Note: With this configuration, the Istanbul instrumentation will only be active when
NODE_ENVorBABEL_ENVistest.We recommend using the
cross-envpackage to set these environment variables in yourpackage.jsonscripts in a way that works cross-platform.disable nyc's instrumentation and source-maps:
{ "nyc": { "include": [ "src/*.js" ], "require": [ "babel-register" ], "sourceMap": false, "instrument": false } }
That's all there is to it, better ES6 syntax highlighting awaits:
Support for alternate file extensions (.jsx, .es6)
Supporting file extensions can be configured through either the configuration arguments or with the nyc config section in package.json.
nyc --extension .jsx --extension .es6 npm test{
"nyc": {
"extension": [
".jsx",
".es6"
]
}
}Checking coverage
nyc can fail tests if coverage falls below a threshold. After running your tests with nyc, simply run:
nyc check-coverage --lines 95 --functions 95 --branches 95nyc also accepts a --check-coverage shorthand, which can be used to
both run tests and check that coverage falls within the threshold provided:
nyc --check-coverage --lines 100 npm testThe above check fails if coverage falls below 100%.
Running reports
Once you've run your tests with nyc, simply run:
nyc reportTo view your coverage report:
you can use any reporters that are supported by istanbul:
nyc report --reporter=lcovExcluding files
You can tell nyc to exclude specific files and directories by adding
an nyc.exclude array to your package.json. Each element of
the array is a glob pattern indicating which paths should be omitted.
Globs are matched using micromatch.
For example, the following config will exclude any files with the extension .spec.js,
and anything in the build directory:
{
"nyc": {
"exclude": [
"**/*.spec.js",
"build"
]
}
}Note: Since version 8.0 if you add a "exclude" array the
node_modulesfolder is not automatically excluded, you will need to explicitly add it to your exclude arrayNote: exclude defaults to
['test', 'test{,-*}.js', '**/*.test.js', '**/__tests__/**'], which would excludetest/__tests__directories as well astest.js,*.test.js, andtest-*.jsfiles. Specifying your own exclude property overrides these defaults.
Including files
As an alternative to providing a list of files to exclude, you can provide
an include key to specify specific files that should be covered:
{
"nyc": {
"include": ["**/build/umd/moment.js"]
}
}Note: include defaults to
['**']
Include reports for files that are not required
By default nyc does not collect coverage for files that have not
been required, run nyc with the flag --all to enable this.
Require additional modules
The --require flag can be provided to nyc to indicate that additional
modules should be required in the subprocess collecting coverage:
nyc --require babel-register --require babel-polyfill mocha
Caching
You can run nyc with the optional --cache flag, to prevent it from
instrumenting the same files multiple times. This can significantly
improve runtime performance.
Configuring nyc
Any configuration options that can be set via the command line can also be specified in the nyc stanza of your package.json (these will not affect nyc subcommands):
{
"description": "These are just examples for demonstration, nothing prescriptive",
"nyc": {
"lines": 99,
"statements": 99,
"functions": 99,
"branches": 99,
"include": [
"src/**/*.js"
],
"exclude": [
"src/**/*.spec.js"
],
"reporter": [
"lcov",
"text-summary"
],
"require": [
"./test/helpers/some-helper.js"
],
"extension": [
".jsx"
],
"cache": true,
"all": true,
"check-coverage": true,
"report-dir": "./alternative"
}
}Instrumenting source files
nyc's instrument command can be used to instrument
source files outside of the context of your unit-tests:
instrument the entire ./lib folder:
nyc instrument ./lib ./output
Process tree information
nyc is able to show you all Node processes that are spawned when running a test script under it:
$ nyc --show-process-tree npm test
3 passed
----------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files | 100 | 100 | 100 | 100 | |
index.js | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|----------------|
nyc
└─┬ /usr/local/bin/node /usr/local/bin/npm test
└─┬ /usr/local/bin/node /path/to/your/project/node_modules/.bin/ava
└── /usr/local/bin/node /path/to/your/project/node_modules/ava/lib/test-worker.js …Integrating with coveralls
coveralls.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 --saveupdate the scripts in your package.json to include these bins:
{ "script": { "test": "nyc tap ./test/*.js", "coverage": "nyc report --reporter=text-lcov | coveralls" } }For private repos, add the environment variable
COVERALLS_REPO_TOKENto travis.add the following to your
.travis.yml:after_success: npm run coverage
That'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?.
Integrating with codecov
nyc npm test && nyc report --reporter=text-lcov > coverage.lcov && codecov
codecov is a great tool for adding coverage reports to your GitHub project, even viewing them inline on GitHub with a browser extension:
Here's how to get nyc integrated with codecov and travis-ci.org:
add the codecov and nyc dependencies to your module:
npm install codecov nyc --save-devupdate the scripts in your package.json to include these bins:
{ "script": { "test": "nyc tap ./test/*.js", "coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov" } }For private repos, add the environment variable
CODECOV_TOKENto travis.add the following to your
.travis.yml:after_success: npm run coverage
That's all there is to it!
9 years ago