question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Karma Code Coverage

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
branparcommented, Aug 1, 2016

Also sometimes I get the following error in my console:

ERROR in ./scripts/modules/[module-name].js
Module not found: Error: a dependency to an entry point is not allowed
@ ./scripts/modules/[module-name].js

I am also not sure what this error means.

0reactions
jessiehancommented, Dec 20, 2016

same problem

Read more comments on GitHub >

github_iconTop Results From Across the Web

Coverage - Karma test runner
Karma can generate code coverage using awesome Istanbul. If you want to generate the coverage, you need to configure up to three parts:....
Read more >
A Karma plugin. Generate code coverage. - GitHub
Generate code coverage using Istanbul. Installation. The easiest way is to install karma-coverage as a devDependency , by running.
Read more >
karma-coverage - npm
A Karma plugin. Generate code coverage.. Latest version: 2.2.0, last published: a year ago. Start using karma-coverage in your project by ...
Read more >
Chapter 23 Code Coverage | Angular | Jasmine | Karma Unit
Code Coverage is a way of determining the part of the code which is left out of testing or not tested properly. Karma...
Read more >
64. Add coverage Reports using karma-coverage plugin in the ...
In this video we will see how to add the coverage reports using Karma - coverage plugin and Istanbul - Jasmine TestingUnit Testing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found