Search code examples
node.jscode-coveragenyc

How do I generate code coverage for some Node.JS code without tests?


I have a build/ folder that gets autogenerated by a babel process in a package.json. It has several .js files, including in sub-folders. In the root is a file, main.js, which is something of a demo / testbed for the project, that instantiates various ES6 classes and tries out various functions. It currently runs without crashing.

Call it a poor man's end-2-end test. I'm trying to move quickly with what could be throw away code.

I don't have any formal tests. I don't want to write any formal tests for this codebase. But I am interested in knowing how much of the code in build/ is being touched currently by my demo, main.js.

  1. How can I generate a code coverage report for this scenario, using nyc?
  2. If that's not actually easy (all tutorials I see seem to involve instrumenting pre-existing unit tests from a mainstream testing framework), what nyc alternative would make this easy?

I tried

npm install nyc --save-dev
npx nyc node build/main.js

but it claimed 0 lines/files.


Solution

  • With thanks to What is instrumentation in nyc istanbul?, it was actually simple. From the root, where my package.json and build/ folder were:

    npx nyc instrument build coverage
    npx nyc --reporter=text --report-dir=./nyc_output node build/main.js
    

    all necessary folders (coverage, nyc_output) were auto-created (though it made .nyc_output/ for some reason)