karma-runner / karma-runner/karma-coverage

Karma Code Coverage

Open
#244 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
771
Forks
242
PR merge metrics
No merged PRs in 30d

Description

Hello,

I am trying to get code coverage to work with Webpack and Karma. I'm running into MANY difficulties which are preventing my company from accurately testing the JS code. Included below is my Karma config file.

```
var config = require('./gulp/config'),
karmaWebpack = require('karma-webpack'),
webpackConfig = require('./gulp/util/webpack-multi-config'),
path = require('path');

var testSrc = './scripts/tests/**/*.tests.js',
jsSrc = './scripts/**(!tests)/*.js';

var karmaConfig = {
frameworks: ['mocha', 'sinon-chai'],
files: [
testSrc,
jsSrc
],
preprocessors: {
testSrc: ['webpack'],
jsSrc: ['webpack', 'coverage']
},
webpack: webpackConfig('test'),
singleRun: 'true',
reporters: ['spec', 'coverage'],
browsers: ['Chrome'],
coverageReporter: {
reporters: [
{ type: 'text' },
{ type: 'text-summary' },
{ type: 'html', dir: './coverage' }
],
},
client: {
captureConsole: false
},
specReporter: {
showSpecTiming: true
},
reportSlowerThan: 25
};

karmaConfig.preprocessors[testSrc] = ['webpack'];
karmaConfig.preprocessors[jsSrc] = ['webpack', 'coverage'];

module.exports = function (config) {
config.set(karmaConfig);
}
```

If I don't include the actual JS source files all the testing works out fine, but code coverage is extremely poor (it's including Webpack garbage and jQuery as part of the coverage). If I include the source files, which is proper, I get an error:

```
Cannot find module "../modules/[module-name]"
at scripts/tests/modules/[module-name].tests.js
```

I don't understand this error as the modules do exist there. It's almost like Karma and Webpack can't be used at the same time. You either have to choose to use Webpack and ES6 modules -OR- do front-end JS testing. Has anyone successfully done this? I've looked for months on this issue and tried posting several different places with no success.

Contributor guide

Open the contributing guide

Research direction

Reproduce the problem with the supplied Karma configuration, focusing on the webpack and coverage preprocessors for testSrc and jsSrc. Inspect how Karma, karma-webpack, and coverage handle the source modules, then verify that the tests load successfully and coverage excludes Webpack-generated code and jQuery.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, webpack
Domain
frontend, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.