Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage for all files using karma and includeAllSources: true #29

Closed
davidklassen opened this issue Aug 9, 2016 · 6 comments
Closed

Comments

@davidklassen
Copy link

Hello,

I'm using a setup with karma+jasmine+babelify for testing, my directory structure looks like this:

├── src
│   ├── moduleA.js
│   └── moduleB.js
└── test
    ├── config
    ├── functional
    └── unit

This is the karma.conf:

module.exports = (config) => {
    config.set({
        basePath: '../../',
        frameworks: ['browserify', 'jasmine'],
        files: [
            'test/unit/**/*.test.js',
        ],
        reporters: ['spec', 'coverage'],
        preprocessors: {
            'test/unit/**/*.test.js': ['browserify'],
        },
        browserify: {
            debug: true,
            transform: [['babelify', { plugins: ['istanbul'] }]],
        },
        coverageReporter: {
            dir: 'dist/coverage/unit',
            includeAllSources: true,
            reporters: [
                { type: 'html' },
                { type: 'json', file: 'coverage.json' },
            ],
        },
        browsers: ['PhantomJS'],
        singleRun: true,
    });
};

I'd like to make use of includeAllSources: true option for coverageReporter but can not find a way to have instrumentation for all files in src/ dir.

@jseminck
Copy link

We ran into the same issue. I found this issue on the karma-coverage github: karma-runner/karma-coverage#125

Unfortunately this has not been resolved yet, but one user recommended the plugin https://www.npmjs.com/package/karma-coverage-allsources - this is working for us.

Give that one a try!

@sontek
Copy link

sontek commented Aug 20, 2016

I just ran into this issue as well. What is the process for getting it to work? Should someone port the karma-coverage-allsources code? It didn't work for me but some people seem to have success with it.

@joyfulelement
Copy link

Same issue. But with webpack instead of browserify with the setup below.

I tried to use the includeAllSources: true flag in coverageReporter of karma.conf.js, however the generated output doesn't include the untested files. It appears that the similar problem could be solved with isparta-loader (karma-runner/karma-coverage#192) though I was under the impression that isparta-loader package is deprecated in favor of babel-plugin-istanbul?

What would be the proper setup for babel-plugin-istanbul in order to get the includeAllSources to work? Any suggestions would be appreciated.

Here is the .babelrc

{
  ...
  "env": {
    "test": {
      "plugins": [
        [
          "istanbul",
          {
            "exclude": [
              "**/__tests__/**"
            ]
          }
        ]
      ]
    }
  }

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

...
    "babel-plugin-istanbul": "2.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 }
      ]
    },
...
})

@bcoe
Copy link
Member

bcoe commented Oct 30, 2016

@joyfulelement we've added support for --all using nyc, would this work for this use-case?

#4

@wezleytsai
Copy link

How would one use the --all flag through karma config?

@bcoe
Copy link
Member

bcoe commented Jul 22, 2017

@wezleytsai @joyfulelement @sontek @davidklassen there's an approach outlined here, for covering all files using Karma:

#105

For technologies other than Karma, there are various approaches to achieving coverage of untested files:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants