Connect middleware for Istanbul to handle server-side as well as browser coverage use-cases for node apps
Connect middleware for getting code coverage numbers in functional tests for nodejs apps using istanbul.
Run the sample app at
test/app to get a feel for how this works.
All of this is experimental and is known to work for narrow use-cases such as an express3 app. YMMV.
require()in the server process
var im = require'istanbul-middleware'isCoverageEnabled = processenvCOVERAGE == "true"; // or a mechanism of your choice//before your code is require()-ed, hook the loader for coverageif isCoverageEnabledconsole.log'Hook loader for coverage - ensure this is not production!';imhookLoader__dirname;// cover all files except under node_modules// see API for other options// now require the rest of your codevar stuff = require'./lib'express = require'express'app = express;// set up basic middleware// ...// add the coverage handlerif isCoverageEnabled//enable coverage endpoints under /coverageappuse'/coverage' imcreateHandler;//add your router and other endpoints//...applisten80;
The above snippet adds the following endpoints to your app under
||Dynamic code coverage HTML report showing live coverage. Clickable with drill-downs just like the static version|
||Reset coverage to baseline numbers|
||Download a zip file with coverage JSON, HTML and lcov reports|
Allows you to post a coverage object for client-side code coverage from the browser.
Must be a JSON object with a
POST /clientendpoint above) using the
window.__coverage__object. You need to figure out how to do this using your favorite test runner.
The API for this is highly experimental given the number of moving parts. But, it roughly looks like this:
var path = require'path'im = require'istanbul-middleware';// do the server side stuff as described above// add a client handler at the appropriate place// (before your static handler, for example)// all JS files under here will be sent instrumented to the browser// see API below for additional options (e.g. ignoring framework code)appuseimcreateClientHandler__dirname;// however, it will only reliably work for simple cases// such as absolute URLs to the JS.// you still need to post the coverage numbers to the//server from your browser tests
You can write your own custom middleware and completely ignore this library's client handler. As in:
appuseif isJSRequiringCoveragereqvar file = getFilePathreq //translate request to file name to be deliveredcode = readTheCodeFromFilefile //use async for a real implementationinstrumenter = imgetInstrumenter;ressendinstrumenterinstrumentSynccode file;//exception handling omitted for brevityelsenext;;
require for coverage using istanbul.
rootOrMatcher can be:
fn(filePath)that should return
truewhen the supplied
filePathshould be covered and
instrumenterOpts is an optional object with additional options to be passed to the istanbul instrumenter. See the
API docs in istanbul for more information. In addition, options can also contain the
postLoadHook key that is
returns connect middleware that exposes additional endpoints for coverage. Endpoints exposed are documented in the summary.
opts is optional and currently only supports one flag.
resetOnGet- boolean to allow resets of coverage to baseline numbers using
GETin addition to
returns connect middleware similar to the
static middleware to return instrumented JS to the client.
The default behavior of the middleware is to intercept all
instrumented version by deriving the path of the file from the URL, instrumenting the code and sending the
instrumented version in the response.
opts is an optional object with the following supported keys:
matcher- a function of the form
fn(request)that returns true if instrumentation is required and false otherwise.
pathTransformer- a function of the form
fn(request)that inspects the request URL and returns the physical path to the JS file on the filesystem.
An example of a matcher function could be:
var parsed = require'url'parserequrl;return parsedpathname && parsedpathnamematch/\.js$/ && !parsedpathnamematch/jquery/;
For all other cases where the client handler provided by this library is not good enough, just write your own middleware as documented in the summary.
returns the instrumenter object that is created as a side-effect of the
hookLoader call. Useful for custom
client-side instrumentation to ensure that the instrumentation is done with the same options for all code.
The following third-party libraries are used by this module: