Giter VIP home page Giter VIP logo

nyc's Introduction

nyc

Build Status Coverage Status NPM version Windows Tests

nyc npm test

a code coverage tool built on istanbul that works for applications that spawn subprocesses.

Instrumenting Your Code

Simply run your tests with nyc, and it will collect coverage information for each process and store it in .nyc_output.

nyc npm test

you can pass a list of Istanbul reporters that you'd like to run:

nyc --reporter=lcov --reporter=text-lcov npm test

If you're so inclined, you can simply add nyc to the test stanza in your package.json:

{
  "script": {
    "test": "nyc tap ./test/*.js"
  }
}

Support For Custom Require Hooks (Babel! ES2015!)

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.

Checking Coverage

nyc exposes istanbul's check-coverage tool. After running your tests with nyc, simply run:

nyc check-coverage --lines 95 --functions 95 --branches 95

This feature makes it easy to fail your tests if coverage drops below a given threshold.

Running Reports

Once you've run your tests with nyc, simply run:

nyc report

To view your coverage report:

you can use any reporters that are supported by istanbul:

nyc report --reporter=lcov

Excluding Files

You can tell nyc to exclude specific files and directories by adding an config.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

In addition to patterns specified in the package, nyc will always exclude files in node_modules.

For example, the following config will exclude all node_modules, any files with the extension .spec.js, and anything in the build directory:

{"config": {
  "nyc": {
    "exclude": [
      "**/*.spec.js",
      "build"
    ]
  }
}}

Note: exclude defaults to ['test', 'test{,-*}.js'], which would exclude the test directory as well as test.js and test-*.js files

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:

{"config": {
  "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-core/register --require babel-polyfill mocha

Configuring Istanbul

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: true

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:

  1. add the coveralls and nyc dependencies to your module:
npm install coveralls nyc --save
  1. update the scripts in your package.json to include these bins:
{
  "script": {
    "test": "nyc tap ./test/*.js",
    "coverage": "nyc npm test && nyc report --reporter=text-lcov | coveralls",
  }
}
  1. For private repos, add the environment variable COVERALLS_REPO_TOKEN to travis.

  2. 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?.

nyc's People

Contributors

bcoe avatar isaacs avatar jamestalmage avatar jasisk avatar lalem001 avatar novemberborn avatar rmg avatar shackpank avatar zertosh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.