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.

Coverage for all files using karma and includeAllSources: true

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
joyfulelementcommented, Aug 31, 2016

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 (https://github.com/karma-runner/karma-coverage/issues/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 }
      ]
    },
...
})
0reactions
bcoecommented, Jul 22, 2017

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

https://github.com/istanbuljs/babel-plugin-istanbul/issues/105

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Karma JS -- How to Include all All Sources? - Stack Overflow
It includes files specified by pattern to coverage statistic. ... You just need to add includeAllSources: true to your coverageReporter , the Reporter ......
Read more >
Coverage reporter is asking for `lcov.info` file if ... - YouTrack
I'm using Intellij Idea + karma plugin to run karma tests. Namely, I want to run test coverage. The issue is that when...
Read more >
How to get karma-coverage (istanbul) to check coverage of ALL ...
When I run karma, the coverage preprocessor & reporter run, but it only checks the files that already have specs written. I want...
Read more >
Configuration File - Karma
All of the configuration options, which specify file paths, use the ... If true, Karma runs the tests inside the original window without...
Read more >
istanbuljs/nyc - Gitter
We ended up with mocha, requiring babel-register, and using istanbul 1.0.0-alpha.2 with include-all-sources: true for code coverage.
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