Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Karma coverage always coming empty

I've been trying to run karma coverage for a couple of days now only to find an empty blank page as below. Karma coverage empty report

Here's my configuration:

var path = require('path');

var webpackConfig = require('./webpack.common');

module.exports = function (config) {
  var _config = {
    basePath: '',
    frameworks: ['jasmine'],
    files: [
      { pattern: './karma-shim.js', watched: false }
    ],
    exclude: [],
    preprocessors: {
      './karma-shim.js': ['webpack', 'sourcemap', 'coverage']
    },
    client: {
      captureConsole: false
    },
    webpack: webpackConfig,

    webpackMiddleware: {
      stats: 'errors-only'
    },

    coverageReporter: {
      dir: 'coverage/',
      reporters: [{
        type: 'json',
        dir: 'coverage',
        subdir: 'json',
        file: 'coverage-final.json'
      }]
    },

    remapIstanbulReporter: {
      src: 'coverage/json/coverage-final.json',
      reports: {
        lcovonly: 'coverage/json/lcov.info',
        html: 'coverage/html',
        'text': null
      },
      timeoutNotCreated: 1000, // default value
      timeoutNoMoreFiles: 1000 // default value
    },

    webpackServer: {
      noInfo: true // please don't spam the console when running in karma!
    },
    reporters: ["mocha", "coverage", "karma-remap-istanbul"],
    port: 9876,
    colors: true,
    logLevel: config.LOG_ERROR,
    autoWatch: false,
    browsers: ['PhantomJS'], // you can also use Chrome

    singleRun: true
  };

  config.set(_config);

};

And here's my karma-shim.js file

Error.stackTraceLimit = Infinity;
require('es6-shim');
require('reflect-metadata');
require('ts-helpers');
require('zone.js/dist/zone');
require('zone.js/dist/long-stack-trace-zone');
require('zone.js/dist/jasmine-patch');
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');

var appContext = require.context('./app', true, /\.spec\.ts/);
appContext.keys().forEach(appContext);

var testing = require('@angular/core/testing');
var browser = require('@angular/platform-browser-dynamic/testing');

testing.setBaseTestProviders(browser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, browser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);

Folder Structure is as follows: folder structure

Any idea what am i missing here? Help much appreciated.

Thanks

like image 222
Joe Saad Avatar asked Aug 30 '16 16:08

Joe Saad


People also ask

Is Karma coverage Istanbul reporter deprecated?

Starting Angular 11, the karma-coverage-istanbul-reporter has been deprecated.

What is code coverage in angular?

Code coverage, also called test coverage, tells you which parts of your code are executed by running the unit and integration tests. Code coverage is typically expressed as percent values, for example, 79% statements, 53% branches, 74% functions, 78% lines.

What is karma config js file?

The Karma configuration file can be written in JavaScript, CoffeeScript, or TypeScript and is loaded as a regular Node. js module. Unless provided as argument, the Karma CLI will look for a configuration file at. ./karma.conf.js. ./karma.conf.coffee.


1 Answers

Your karma config is clearly missing a reference to the source.

Please modify config as follows:

module.exports = function (config) {
    var _config = {
        [...]
        preprocessors: {
            // Remove coverage as preprocessor for your tests - 
            // Istambul (runs coverage behind the scene for karma) will 
            // instrumentate this
            './karma-shim.js': ['webpack', 'sourcemap'],
            // Add path to your source (.js or .ts - depands on your project)
            './index.ts': [ 'coverage' ]
        },
        [...]
    },
    [...]
}

Explanation: coverage tests your code against your unit tests - you need to supply entry point for your code to get Karma analyse coverage.

Extras - adding karma-coverage plugin:

module.exports = function (config) {
    var _config = {
        [...]
        plugins: [
            require( 'karma-coverage' )
        ],
        [...]
    },
    [...]
}

Extras - karma & typescript files:

module.exports = function (config) {
    var _config = {
        [...]
        plugins: [
            require( '@angular/cli/plugins/karma' )
        ],
        [...]
    },
    [...]
    preprocessors: {
        './src/test.ts': [ '@angular/cli' ],
        './index.ts': [ 'coverage' ]
    },
    [...]
    mime: {
       'text/x-typescript': [ 'ts', 'tsx' ]
    },
    [...]
}
like image 61
Fill Avatar answered Sep 20 '22 14:09

Fill