karma-runner / karma-coverage

A Karma plugin. Generate code coverage.
MIT License
770 stars 247 forks source link

Problem with includeAllSources, webpack and isparta-instrumenter #192

Open jbe456 opened 8 years ago

jbe456 commented 8 years ago

Using the isparta-instrumenter with webpack and karma-coverage I can't make the option includeAllSources to work.

Here is my webpack config:

{
    /** ... */,
    module: {
      preLoaders: [
        {
          test: /\.jsx?$/,
          include: path.resolve('src'),
          loader: 'isparta-instrumenter'
        }
      ],
      loaders: /** ... */
    },
    /** ... */
  }

Here is the karma.conf.js:

config.set({
    basePath: path.resolve('.'),
    frameworks: ['mocha'],
    files: [
      'tests/EntryPoint.js'
    ],
    preprocessors: {
      'tests/**/*.js': ['webpack']
    },
    webpack: webpackConfig,
    webpackMiddleware: {
      stats: statsOptions
    },
    reporters: ['progress', 'coverage'],
    coverageReporter: {
      type: 'html',
      dir: 'coverage',
      includeAllSources: true
    },
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    captureTimeout: 60000,
    singleRun: true,
    plugins: [
      'karma-webpack',
      'karma-mocha',
      'karma-coverage',
      'karma-chrome-launcher'
    ]
  });

I get the html output but the untested files are missing. Did anybody make this option work with isparta-instrumenter and webpack? I'm pretty sure there is no issue here but I can't find the correct configuration.

I tried adding coverage as a preprocessor for my source file but it seems to fail because they are ES6 or JSX files.

andrewwakeling commented 8 years ago

I'm facing the same issue. @jbe456, were you able to solve your problem?

jbe456 commented 8 years ago

No @andrewwakeling I didn't spend much time on this. I still have the issue although I did not try with more recent versions since then.

icfantv commented 8 years ago

@andrewwakeling, i found a workaround just using the isparta-loader instead of the isparta-instrumenter loader. I followed the instructions here.

Basically, all I had to do was

  1. swap out the preloader from isparta-instrumenter-loader to isparta-loader in package.json.
  2. in the file you've specified by your preprocessor object key (e.g., mine is src/tests.webpack.js), add the following:
    const tests = require.context(".", true, /.spec$/);
    tests.keys().forEach(tests);

    const components = require.context('.', true, /index.js$/); components.keys().forEach(components);
  3. change your webpack module preloader from isparta-instrumenter-loader to isparta-loader.

Running your tests should result in a significant change in your coverage numbers.

phun-ky commented 8 years ago

I tried to do that, but I keep getting errors I can't solve:

ERROR in ./~/normalize.css/normalize.css
Module parse failed: /home/alexander/Workspace/MyProject/node_modules/normalize.css/normalize.css Unexpected token (9:5)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (9:5)

ERROR in ./~/svg-sprite-loader!./src/client/images/icons/motor.svg
Module build failed: TypeError: Must be an object
    at exports.objectToAttrString (/home/alexander/Workspace/MyProject/node_modules/svg-sprite-loader/lib/utils.js:6:11)
    at SVGDocument.toString (/home/alexander/Workspace/MyProject/node_modules/svg-sprite-loader/lib/svg-document.js:29:18)
    at Object.module.exports (/home/alexander/Workspace/MyProject/node_modules/svg-sprite-loader/index.js:47:17)

The complete error log: https://gist.github.com/phun-ky/8f58b18061ae908e6a25e332114cafe5

Config: https://gist.github.com/phun-ky/72b3fd81556fc465144f15a873bdebb4 loadtests.js: https://gist.github.com/phun-ky/4dc5c2dd2dfba6a6b4d1d2be0e1b445d

client/index.js is the entry point for the client code in webpack

phun-ky commented 8 years ago

Got it working now, with this setup: https://gist.github.com/phun-ky/715386135c588a05efa3fd33af3da436

Had to NOT include the first js file that renders the client (rookie mistake), and Only used isparta-loader in the preLoaders section

kopach commented 4 years ago

Hi there. For anyone still interested in this topic, Here is a tool, which helps to add untested files to coverage report. It works fine with both JavaScript and TypeScript files. https://github.com/kopach/karma-sabarivka-reporter As per my understanding, karma has limitation in detecting all source files, in case we pass only 1 file as starting point to our tests. E.g. as from example above:

files: [
  'tests/EntryPoint.js'
],

This tool overcomes this limitation.