Permalink
Switch branches/tags
gh-pages greenkeeper/babel-eslint-8.2.3 greenkeeper/babel-eslint-8.2.4 greenkeeper/babel-eslint-8.2.5 greenkeeper/babel-eslint-8.2.6 greenkeeper/babel-eslint-9.0.0 greenkeeper/babel-eslint-10.0.0 greenkeeper/babel-eslint-10.0.1 greenkeeper/babel-eslint-pin-8.2.2 greenkeeper/babel-loader-7.1.5 greenkeeper/babel-loader-8.0.0-beta.3 greenkeeper/babel-loader-8.0.0 greenkeeper/babel-loader-8.0.1 greenkeeper/babel-loader-8.0.2 greenkeeper/babel-loader-8.0.3 greenkeeper/babel-loader-8.0.4 greenkeeper/babel-loader-pin-7.1.4 greenkeeper/babel-plugin-istanbul-4.1.6 greenkeeper/babel-plugin-istanbul-pin-4.1.5 greenkeeper/babel-plugin-lodash-3.3.3 greenkeeper/babel-plugin-lodash-3.3.4 greenkeeper/babel-plugin-lodash-pin-3.3.2 greenkeeper/bdd-lazy-var-2.2.4 greenkeeper/bdd-lazy-var-2.2.5 greenkeeper/bdd-lazy-var-2.2.6 greenkeeper/bdd-lazy-var-2.3.0 greenkeeper/bdd-lazy-var-2.4.0 greenkeeper/bdd-lazy-var-2.4.1 greenkeeper/bdd-lazy-var-2.4.2 greenkeeper/bdd-lazy-var-2.4.3 greenkeeper/bdd-lazy-var-2.4.4 greenkeeper/bdd-lazy-var-2.5.0 greenkeeper/bdd-lazy-var-pin-2.2.3 greenkeeper/chokidar-2.0.4 greenkeeper/chokidar-pin-2.0.3 greenkeeper/cross-env-5.2.0 greenkeeper/cross-env-pin-5.1.6 greenkeeper/css-loader-1.0.0 greenkeeper/css-loader-1.0.1 greenkeeper/css-loader-2.0.0 greenkeeper/eslint-5.0.0 greenkeeper/eslint-5.0.1 greenkeeper/eslint-config-airbnb-base-13.0.0 greenkeeper/eslint-plugin-flowtype-2.46.2 greenkeeper/eslint-plugin-flowtype-2.46.3 greenkeeper/eslint-plugin-flowtype-2.47.0 greenkeeper/eslint-plugin-flowtype-2.47.1 greenkeeper/eslint-plugin-flowtype-2.48.0 greenkeeper/eslint-plugin-flowtype-2.49.0 greenkeeper/eslint-plugin-flowtype-2.49.1 greenkeeper/eslint-plugin-flowtype-2.49.2 greenkeeper/eslint-plugin-flowtype-2.49.3 greenkeeper/eslint-plugin-flowtype-2.49.4 greenkeeper/eslint-plugin-flowtype-2.50.0 greenkeeper/eslint-plugin-flowtype-2.50.1 greenkeeper/eslint-plugin-flowtype-2.50.2 greenkeeper/eslint-plugin-flowtype-2.50.3 greenkeeper/eslint-plugin-flowtype-3.0.0 greenkeeper/eslint-plugin-flowtype-3.1.0 greenkeeper/eslint-plugin-flowtype-3.1.1 greenkeeper/eslint-plugin-flowtype-3.1.2 greenkeeper/eslint-plugin-flowtype-3.1.3 greenkeeper/eslint-plugin-flowtype-3.1.4 greenkeeper/eslint-plugin-flowtype-3.2.0 greenkeeper/eslint-plugin-flowtype-errors-3.6.0 greenkeeper/eslint-plugin-flowtype-errors-pin-3.5.1 greenkeeper/eslint-plugin-flowtype-pin-2.46.1 greenkeeper/eslint-plugin-import-2.13.0 greenkeeper/eslint-plugin-import-pin-2.12.0 greenkeeper/fast-async-7.0.0 greenkeeper/fast-async-7.0.2 greenkeeper/fast-async-7.0.3 greenkeeper/fast-async-7.0.4 greenkeeper/fast-async-7.0.5 greenkeeper/fast-async-7.0.6 greenkeeper/flow-bin-0.69.0 greenkeeper/flow-bin-0.70.0 greenkeeper/flow-bin-0.71.0 greenkeeper/flow-bin-0.73.0 greenkeeper/flow-bin-0.74.0 greenkeeper/flow-bin-0.75.0 greenkeeper/flow-bin-0.76.0 greenkeeper/flow-bin-0.77.0 greenkeeper/flow-bin-0.78.0 greenkeeper/flow-bin-0.79.0 greenkeeper/flow-bin-0.79.1 greenkeeper/flow-bin-0.80.0 greenkeeper/flow-bin-0.81.0 greenkeeper/flow-bin-0.82.0 greenkeeper/flow-bin-0.83.0 greenkeeper/flow-bin-0.84.0 greenkeeper/flow-bin-0.85.0 greenkeeper/flow-bin-0.86.0 greenkeeper/flow-bin-0.87.0 greenkeeper/flow-bin-0.88.0 greenkeeper/fs-extra-6.0.1 greenkeeper/fs-extra-7.0.0 greenkeeper/fs-extra-7.0.1 greenkeeper/gh-pages-2.0.0 greenkeeper/gh-pages-2.0.1 greenkeeper/glob-7.1.3 greenkeeper/lodash-4.17.11 greenkeeper/lodash-pin-4.17.10 greenkeeper/mocha-5.2.0 greenkeeper/mocha-pin-5.1.1 greenkeeper/node-sass-4.8.3 greenkeeper/node-sass-4.9.0 greenkeeper/node-sass-4.9.1 greenkeeper/node-sass-4.9.2 greenkeeper/node-sass-4.9.3 greenkeeper/node-sass-4.9.4 greenkeeper/node-sass-4.10.0 greenkeeper/node-sass-4.11.0 greenkeeper/normalize-path-3.0.0 greenkeeper/np-3.0.0 greenkeeper/np-3.0.1 greenkeeper/np-3.0.2 greenkeeper/np-3.0.3 greenkeeper/np-3.0.4 greenkeeper/nyc-12.0.1 greenkeeper/nyc-12.0.2 greenkeeper/progress-2.0.1 greenkeeper/progress-2.0.2 greenkeeper/progress-2.0.3 greenkeeper/progress-pin-2.0.0 greenkeeper/sass-loader-7.0.0 greenkeeper/sass-loader-7.0.1 greenkeeper/sass-loader-7.0.2 greenkeeper/sass-loader-7.0.3 greenkeeper/sass-loader-7.1.0 greenkeeper/sinon-5.0.1 greenkeeper/sinon-5.0.2 greenkeeper/sinon-5.0.3 greenkeeper/sinon-5.0.5 greenkeeper/sinon-5.0.6 greenkeeper/sinon-5.0.8 greenkeeper/sinon-5.0.9 greenkeeper/sinon-5.0.10 greenkeeper/sinon-5.1.0 greenkeeper/sinon-5.1.1 greenkeeper/sinon-6.0.0 greenkeeper/sinon-6.0.1 greenkeeper/source-map-support-0.5.5 greenkeeper/strip-ansi-5.0.0 greenkeeper/toposort-1.0.7 greenkeeper/toposort-2.0.1 greenkeeper/toposort-2.0.2 greenkeeper/toposort-pin-1.0.6 greenkeeper/update-to-node-10 greenkeeper/webpack-4.9.1 greenkeeper/webpack-4.10.2 greenkeeper/webpack-4.11.0 greenkeeper/webpack-4.11.1 greenkeeper/webpack-4.12.0 greenkeeper/webpack-4.12.1 greenkeeper/webpack-4.12.2 greenkeeper/webpack-4.13.0 greenkeeper/webpack-4.14.0 greenkeeper/webpack-4.15.0 greenkeeper/webpack-4.15.1 greenkeeper/webpack-4.16.0 greenkeeper/webpack-4.16.1 greenkeeper/webpack-4.16.2 greenkeeper/webpack-4.16.3 greenkeeper/webpack-4.16.4 greenkeeper/webpack-4.16.5 greenkeeper/webpack-4.17.0 greenkeeper/webpack-4.17.1 greenkeeper/webpack-4.17.2 greenkeeper/webpack-4.17.3 greenkeeper/webpack-4.18.0 greenkeeper/webpack-4.18.1 greenkeeper/webpack-4.19.0 greenkeeper/webpack-4.19.1 greenkeeper/webpack-4.20.0 greenkeeper/webpack-4.20.1 greenkeeper/webpack-4.20.2 greenkeeper/webpack-4.21.0 greenkeeper/webpack-4.22.0 greenkeeper/webpack-4.23.0 greenkeeper/webpack-4.23.1 greenkeeper/webpack-4.24.0 greenkeeper/webpack-4.25.0 greenkeeper/webpack-4.25.1 greenkeeper/webpack-4.26.0 greenkeeper/webpack-4.26.1 greenkeeper/webpack-4.27.0 greenkeeper/webpack-4.27.1 greenkeeper/webpack-pin-4.9.0 greenkeeper/worker-loader-2.0.0 greenkeeper/write-file-webpack-plugin-4.3.0 greenkeeper/write-file-webpack-plugin-4.3.1 greenkeeper/write-file-webpack-plugin-4.3.2 greenkeeper/write-file-webpack-plugin-4.4.0 greenkeeper/write-file-webpack-plugin-4.4.1 greenkeeper/write-file-webpack-plugin-4.5.0 greenkeeper/write-file-webpack-plugin-pin-4.2.0 greenkeeper/yargs-12.0.1 master
Nothing to show
Find file Copy path
02864e2 Mar 19, 2018
3 contributors

Users who have contributed to this file

@zinserjan @TerrenceLJones @astorije
172 lines (127 sloc) 5.65 KB

Code Coverage with mocha-webpack

This guide will show you how to setup code coverage with mocha-webpack and get a report like the following.

code coverage report Yes, this is a very poor sample report for just two files...

For a generic setup that works with Babel, TypeScript and others, you just need the following tools:

  • nyc: CLI for istanbul that collects coverage data & generates coverage reports
  • istanbul-instrumenter-loader: Loader that wraps your code with hooks to track coverage when your code get's executed.

First of all we install both with

$ npm install --save-dev nyc istanbul-instrumenter-loader

Then your package.json should contain the installed modules.

package.json

{
  ...
  "devDependencies": {
    "cross-env": "^3.1.4",
    ...
    "istanbul-instrumenter-loader": "^1.2.0",
    ...
    "nyc": "^10.0.0",
    ...
  },
  ...
}

Note: cross-env is here also installed, which set environment variables across platforms.

As second step we define a npm script to run our tests with code coverage.

package.json

{
  "scripts": {
    "test": "mocha-webpack --watch",
    "test-ci": "mocha-webpack",
    "cover": "cross-env NODE_ENV=coverage nyc --reporter=lcov --reporter=text npm run test-ci"
  },
}

This allows us to get coverage reports of our codebase with the command npm run cover.

Note: mocha-webpack in this sample is preconfigured via the mocha-webpack.opts file.

As next we need to configure nyc within our package.json:

package.json

{
  ...
  "nyc": {
    "include": [
      "src/**/*.js"
    ],
    "instrument": false,
    "sourceMap": false
   },
   ...
}
  • include contains pattern for the location of your source files. Only the files that are listed in this list, are covered.
  • instrument: false stops nyc from instrumenting your code, that's the task of loader
  • sourceMap: false is disabled for the same reason like instrument

When you start npm run cover now, you get something like this:

code coverage unknown files

Now it's time to setup istanbul-instrumenter-loader to get proper code coverage reports. You just need to add istanbul-instrumenter-loader to the loaders list in your webpack configuration.

webpack.config-test.js

var nodeExternals = require('webpack-node-externals');
var isCoverage = process.env.NODE_ENV === 'coverage';

module.exports = {
  output: {
    // use absolute paths in sourcemaps (important for debugging via IDE)
    devtoolModuleFilenameTemplate: '[absolute-resource-path]',
    devtoolFallbackModuleFilenameTemplate: '[absolute-resource-path]?[hash]'
  },
  module: {
    rules: [].concat(
      isCoverage ? {
          test: /\.(js|ts)/,
          include: path.resolve('src'), // instrument only testing sources with Istanbul, after ts-loader runs
          loader: 'istanbul-instrumenter-loader'
      }: [],
      {
          test: /.js$/,
          exclude: /(node_modules|bower_components)/,
          loader: 'babel-loader',
      },
      {
          test: /\.ts$/,
          exclude: /(node_modules|bower_components)/,
          loader: 'ts-loader'
      }
    ),
    // ...
  },
  target: 'node',  // webpack should compile node compatible code
  externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
  devtool: "inline-cheap-module-source-map"
};

In this config we add istanbul-instrumenter-loader only when we want coverage reports. The loader must be the first entry in the loaders array to ensure that it gets applied as last. Alternatively you can also use the enforce option.

Another important detail is that the include option should be configured in the same way (but webpack compatible) as in the package.json. istanbul-instrumenter-loader does not respect the value in the package.json, that's why you need to configure it again.

When you run npm run cover again you should see something like this - dependent on your code:

code coverage success

And you should get an html report (located at ./coverage/lcov-report/index.html) that looks like the one from the intro.

Generate coverage reports for the whole codebase

Only files that were executed within your test run generate code coverage reports. Normally you start your tests with just the test files as entries.

But when you want code coverage reports for the whole codebase, you need to import the rest of the code. It happens very often, that some files doesn't have any tests and doesn't impact the code coverage negatively.

That's not the desired behavour.

Let's assume that we have a directory for source files with name src and a folder for tests with name test in the project root, like the following:

project
  - src
    - module1.js
    - module2.js
  - test
    - module1.test.js

As you can see we have two files module1.js and module2.js in our src folder. And more important only module1.js has a related unit test file. module2.js doesn't have any tests.

When we run our tests now with the following command...

$ mocha-webpack "test/**/*.js"

... module2.js will never be executed.

To fix this we add another entry to make sure that all source files are imported:

$ mocha-webpack "src/**/*.js" "test/**/*.js"

Note: It's recommended to have a CI configuration that imports all source + test files and for developing it's best to import only the tests cause importing code that will never execute causes unnecessary compile time.