André
André

Reputation: 156

How Can I Exclude Spec Files from Code Coverage with Karma using Webpack and Babel?

Problem

I'm working on a small react-redux project configured with webpack, babel, and karma. I added code coverage to karma, but I couldn't find a way to exclude the test files from coverage. So my code coverage has spec files.

How can I exclude these spec files from coverage?

I tried to use a regex expression to exclude the spec files, but since it was being loaded by webpack, it didn't work.

tests.webpack.js

const context = require.context('./src', true, /.+\Spec\.js$/);
context.keys().forEach(context);
module.exports = context;

webpack.config.js

module.exports = {
  entry: './src/index.js',
  output: {
    path: __dirname,
    filename: 'dist/bundle.js'
  },
  devtool: 'source-map',
  resolve: {
    extensions: ['', '.js', '.scss'],
    modulesDirectories: [
      'node_modules',
      'src'
    ]
  },
  module: {
    preLoaders: [
      {
        test: /\.js$/,
        loader: 'eslint-loader',
        exclude: /node_modules/
      }
    ],
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
    ],
  },
};

karma.config.js

var path = require('path');

module.exports = function (config) {
  config.set({
    browsers: ['PhantomJS'],
    singleRun: true,
    frameworks: ['mocha', 'sinon-chai'],
    files: [
      'tests.webpack.js'
    ],

    preprocessors: {
      'tests.webpack.js': ['webpack', 'sourcemap']
    },
    reporters: ['mocha', 'osx', 'coverage'],
    webpack: {
      module: {
        preLoaders: [
          {
            test: /\.js$/,
            exclude: [
              path.resolve('src/'),
              path.resolve('node_modules/')
            ],
            loader: 'babel'
          },
          {
            test: /\.js$/,
            include: path.resolve('src/'),
            loader: 'isparta'
          }
        ]
      }
    },
    webpackServer: {
      noInfo: true
    },
    coverageReporter: {
      type: 'html',
      dir: 'coverage/'
    }
  });
};

Upvotes: 2

Views: 6908

Answers (2)

David Bradshaw
David Bradshaw

Reputation: 13097

This is how I do it on my project, where I have all my tests in __test__ folders included with each component. You should be able to change this to a regex something like this /\.spec.js$/.

karmaConfig.webpack.module.preLoaders = [{
  test    : /\.(js|jsx)$/,
  include : new RegExp(config.dir_client),
  loader  : 'isparta',
  exclude : [
    /node_modules/,
    /__test__/,
  ],
}];

In your case you need to add exclude to this bit of your config.

{
    test: /\.js$/,
    include: path.resolve('src/'),
    loader: 'isparta'
}

Upvotes: 2

chrisblossom
chrisblossom

Reputation: 1

I solved this by filtering out the results I didn't want by placing a .filter() before the .forEach(context).

const includes = require('lodash/includes');
const context = require.context('./src', true, /\.test\.js$/);

context.keys()
  .filter(file => includes(file, './api/') === false)
  .forEach(context);

You could also write it directly into the .forEach().

const includes = require('lodash/includes');
const context = require.context('./src', true, /\.test\.js$/);

context.keys()
  .forEach(file => {
    if (includes(file, './api/') === false) {
      context(file);
    }
  });

If you don't use Lodash, you can use:

file.indexOf('./api/') === -1

Upvotes: 0

Related Questions