1
votes

I have a project with a folder structure like

Project
|--Web
|----Scripts
|------App
|--------feature.js
|------Libs
|------Tests
|--------Specs
|----------spec.js
|--------karma-conf.js

In my karma-conf.js I'm point the coverage preprocessor to ../App/feature.js but this gives me a blank coverage report stating 'No data to display'.

I've tried some other path configurations with no luck. Karma documentation states that the path should be relative to the base path. I can't move the test folder for legacy reasons.

Below is a duplicate of my karma-conf.js

I'd be very grateful for any insight into how the paths work for karma-coverage.

    module.exports = function (config) {
    config.set({
        hostname: 'localhost',

        // base path, that will be used to resolve files and exclude
        basePath: '',

        // frameworks to use
        frameworks: ['jasmine'],

        // list of files / patterns to load in the browser
        files: [
            {
                pattern: '../App/feature.js',
                watched: true,
                served: true,
                included: true
            },
            {
                pattern: 'Specs/spec/*.js',
                watched: true,
                served: true,
                included: true
            }
        ],

        // test results reporter to use
        // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
        reporters: ['progress','coverage'],

        // web server port
        port: 6789,

        // enable / disable colors in the output (reporters and logs)
        colors: true,

        // level of logging
        // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
        logLevel: config.LOG_DEBUG,

        // enable / disable watching file and executing tests whenever any file changes
        autoWatch: true,

        // Continuous Integration mode
        // if true, it capture browsers, run tests and exit
        singleRun: true,

        preprocessors: {
            '**/.html': [],
            '**/*.coffee': [],
            "../App/feature.js": "coverage"
        }
    });
};
2
I have also had some problems with path's in Karma... did you try appending base/ to the path. It worked for me from inside the specs - Sergio

2 Answers

0
votes

Use the following process:

  • Move the karma.conf.js file directly under the Scripts directory
  • Make sure it is in the same level as App so the base path matches
  • Change the mapping to:

    'App/feature.js': 'coverage'
    
0
votes

Use this link as reference: https://jaredtong.com/2016/01/08/how-to-set-up-mocha-chai-sinon-karma-browserify-istanbul-codecov/

I got it to work with this config:

    // Karma configuration
// Generated on Tue Apr 25 2017 13:33:19 GMT-0400 (Eastern Daylight Time)

// Required by Browserify 
var istanbul = require('browserify-istanbul');

module.exports = function(config) {
     'use strict';
  config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
// Include Browserify first. https://www.npmjs.com/package/karma-browserify
frameworks: [ 'browserify', 'jasmine'],


// list of files / patterns to load in the browser
files: [
  'src/**/*.js',
  'spec/**/*.js'
],


// list of files to exclude
exclude: [
],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
    'src/**/*.js': ['browserify'],
    'spec/**/*.js': ['browserify']
},

browserify: {
        debug: true,
        transform: [
            'brfs',
            istanbul({
                ignore: ['**/node_modules/**']
            })
        ]
    },

//
plugins: ['karma-chrome-launcher', 'karma-jasmine', 'karma-coverage', 'karma-firefox-launcher', 'karma-browserify'],

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['coverage'],

// optionally, configure the reporter
coverageReporter: {
    type : 'html',
    dir : 'coverage/',
    includeAllSources: true
},


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity

}) }