karma-runner / karma-coverage

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

includeAllSources doesn't work with webpack and babel-plugin-__coverage__ #249

Open joyfulelement opened 8 years ago

joyfulelement commented 8 years ago

By supplying the includeAllSources: true flag in coverageReporter of karma.conf.js, the generated output doesn't include the untested files with the following setup. This maybe related to #192 , but with the different instrumenter babel-plugin-__coverage__ being used here.

Does anyone had similar experience for how to include untested files in the result and manage to get it to work with babel-plugin-__coverage__? Thanks

Here is the .babelrc

{
  ...
  "env": {
    "test":{
      "plugins": [
        ["__coverage__", {"ignore": "*.+(spec|mock).*"}]
      ]
    }
  }
}

Here is the lineup of the libraries in package.json:

...
    "babel-plugin-__coverage__": "11.0.0",
    "karma": "1.1.1",
    "karma-chrome-launcher": "1.0.1",
    "karma-cli": "1.0.1",
    "karma-coverage": "1.1.1",
    "karma-firefox-launcher": "1.0.0",
    "karma-jasmine": "1.0.2",
    "karma-sourcemap-loader": "0.3.7",
    "karma-spec-reporter": "0.0.26",
    "karma-webpack": "1.7.0",
    "webpack": "1.13.1",
...

Here is the code snippet from karma.conf.js:

config.set({
...
    preprocessors: {
      '../../src/js/**/*.js': [ 'webpack', 'sourcemap' ],
      '../../src/js/**/*.jsx': [ 'webpack', 'sourcemap' ],
      '../../src/js/**/__tests__/*spec.js': [ 'webpack', 'sourcemap' ],
      '../../src/js/**/__tests__/**/*spec.js': [ 'webpack', 'sourcemap' ]
    },

    webpack: {
      module: {
        loaders: [
          {
            test: /\.(js|jsx)$/,
            exclude: /node_modules/,
            loaders: [ 'babel-loader' ] //babel loads jsx and es6-7
          },
          { test: /\.json$/, loader: 'json-loader' }
        ]
      },
      plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin(),
        new webpack.IgnorePlugin(/^fs$/)
      ],
      devtool: 'cheap-module-eval-source-map'
    },

    reporters: [ 'spec', 'coverage' ],

    coverageReporter: {
      reporters: [
        { type: 'html', dir: 'coverage/', subdir: 'report-html', includeAllSources: true },
        { type: 'cobertura', dir: 'coverage/report-cobertura/', file: 'cobertura.txt', includeAllSources: true },
        { type: 'text-summary', includeAllSources: true }
      ]
    },
...
})
jalet commented 8 years ago

I've the same issue, will this get any attention?

bsuh commented 6 years ago

I spent the few day looking into this and here's my solution using babel-plugin-istanbul and babel 7. https://github.com/karma-runner/karma-coverage/issues/125#issuecomment-385261963

babel-plugin-__coverage__ is no longer maintained so it's probably a good idea to switch to babel-plugin-istanbul anyway.

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 quite easy. https://github.com/kopach/karma-sabarivka-reporter Karma has limitation in detecting all source files, in case we pass only 1 file as starting point to our tests. E.g.:

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

This tool overcomes such limitation.

soletan commented 1 year ago

What's the benefit of adding another tool when it's all about karma lacking to see all entry files of your project? Simply add bogus tests for those entry files should be sufficient, e.g. when in an Angular project, test your AppModule for being truthy.

import {AppModule} from './app.module';

describe('App module', () => {
    it('exists', () => {
        expect(AppModule).toBeTruthy();
    })
})

This works well in our case.