Find a good way to get a coverage report with karma?

我们两清 提交于 2020-01-14 06:29:12

问题


I'm using karma-coverage + browserify + angular. The result i get from coverage is that 100% of the files under test are covered which is not true. Any idea how to make this work?

This is my file karma.conf.js :

// Karma configuration

module.exports = function (config) {
    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
        frameworks: ['browserify', 'jasmine'],

        // list of files / patterns to load in the browser
        files: [
            'karma.spec.js',
            'node_modules/es6-shim/es6-shim.js',
            'src/**/*.js',
            'unitest/**/*.js',
            'node_modules/karma-read-json/karma-read-json.js',
            { pattern: 'unitest/mocks/*.json', included: false }
        ],

        // 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: {
            'karma.spec.js': ['browserify'],
            'src/**/*.js': ['browserify']
        },

        browserify: {
            debug: true,
            transform: ['babelify']
        },

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

        // generate coverage report in htm format
        coverageReporter: {
            type : 'json',
            dir : 'report/',
            subdir: './report',
            file: 'coverage-final.json'
        },
        // 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
        // Start these browsers, currently available:
        // - Chrome
        // - ChromeCanary
        // - Firefox
        // - Opera
        // - Safari (only Mac)
        // - PhantomJS
        // - IE (only Windows)
        browsers: ['Chrome', 'PhantomJS'],

        // If browser does not capture in given timeout [ms], kill it
        'captureTimeout': 60000,

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

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

This is my package.json :

{
  "name": "project-name",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "author": "name",
  "devDependencies": {
    "angular-mocks": "^1.5.0",
    "babel": "^6.5.2",
    "babel-preset-es2015": "^6.9.0",
    "babelify": "^7.2.0",
    "browserify": "^13.0.1",
    "envify": "^3.4.0",
    "glob": "^7.0.0",
    "concurrently": "^2.0.0",
    "lite-server": "^2.1.0",
    "typescript": "^1.8.7",
    "jasmine-core": "2.4.1",
    "jasmine": "^2.4.1",
    "jspm": "^0.16.29",
    "karma": "^0.13.21",
    "karma-babel-preprocessor": "^6.0.1",
    "karma-browserify": "^5.0.1",
    "karma-coverage": "^0.5.5",
    "karma-chrome-launcher": "^0.2.2",
    "karma-jasmine": "^0.3.7",
    "karma-notify-reporter": "^0.1.1",
    "karma-read-json": "^1.1.0",
    "karma-sourcemap-loader": "^0.3.7",
    "remap-istanbul": "^0.5.1"
  },
  "scripts": {
    "remap-istanbul": "remap-istanbul -i ./report/coverage/coverage-final.json -o ./report/coverage/html-report -t html",
    "test": "karma start && npm run remap-istanbul"
  },
  "dependencies": {
    "angular": "^v1.5.0",
    "angular-animate": "^1.5.7",
    "angular-route": "^v1.5.0-rc.2",
    "angular-translate": "^2.9.0",
    "angular-ui-bootstrap": "^1.3.3",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "lodash": "^4.14.0",
    "watchify": "^3.7.0"
  },
  "browserify": {
    "transform": [
      [
        "babelify",
        {
          "presets": [
            "es2015"
          ]
        }
      ]
    ]
  }
}

回答1:


Thanks to all of you, i am found the right solution.

Here is my files Karma.conf & package.json :

Karma.conf :

// Karma configuration
'use strict';

let babelify = require( 'babelify' );
let browserify = require('browserify');
let browserifyBabalIstanbul = require('browserify-babel-istanbul');
let isparta = require('isparta');

module.exports = function (config) {
    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
        frameworks: ['jasmine', 'browserify'],

        // list of files / patterns to load in the browser
        files: [
            'karma.spec.js',
            'src/**/*.js',
            'unitest/**/*.js',
            'node_modules/karma-read-json/karma-read-json.js',
            { pattern: 'unitest/mocks/*.json', included: false }
        ],

        // 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: {
            'karma.spec.js': ['browserify'],
            'src/**/*.js': ['browserify']

        },

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

        // Configure coverage reporter
        coverageReporter: {
            reporters:[
                {type: 'html', dir:'coverage/'},
                {type: 'text-summary'},
                {
                    type : 'text',
                    dir : 'coverage/',
                    file : 'coverage.txt',
                    includeAllSources: true
                }
            ],
            check: {
                global: {
                    statements: 60,
                    branches: 60,
                    functions: 60,
                    lines: 60,
                    excludes: [
                        'foo/bar/**/*.js'
                    ]
                },
                each: {
                    statements: 20,
                    branches: 20,
                    functions: 20,
                    lines: 20,
                    excludes: [
                        'other/directory/**/*.js'
                    ],
                    overrides: {
                        'baz/component/**/*.js': {
                            statements: 98
                        }
                    }
                }
            },
            watermarks: {
                statements: [ 50, 75 ],
                functions: [ 50, 75 ],
                branches: [ 50, 75 ],
                lines: [ 50, 75 ]
            }
        },

        // 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
        // Start these browsers, currently available:
        // - Chrome
        // - ChromeCanary
        // - Firefox
        // - Opera
        // - Safari (only Mac)
        // - PhantomJS
        // - IE (only Windows)
        browsers: ['Chrome'],

        // If browser does not capture in given timeout [ms], kill it
        'captureTimeout': 60000,

        browserify: {
            debug: true,
            transform: [
                browserifyBabalIstanbul({
                    instrumenter: isparta,
                    instrumenterConfig: { babel: { presets: ["es2015"] } },
                    ignore: ['**/node_modules/**', '**/unitest/**']
                }),
                [ babelify, { presets: ["es2015"] } ]
            ]
        },

        // Karma plugins loaded
        plugins: [
            'karma-jasmine',
            'karma-babel-preprocessor',
            'karma-browserify',
            'karma-coverage',
            'karma-chrome-launcher'
        ],

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

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

package.json

{
  "name": "project-name",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "author": "name",
  "devDependencies": {
    "angular-mocks": "^1.5.0",
    "babel": "^6.5.2",
    "babel-preset-es2015": "^6.9.0",
    "babelify": "^7.2.0",
    "browserify": "^13.0.1",
    "browserify-babel-istanbul": "^0.3.0",
    "isparta": "^4.0.0",
    "envify": "^3.4.0",
    "glob": "^7.0.0",
    "jasmine": "^2.4.1",
    "jspm": "^0.16.29",
    "karma": "^0.13.21",
    "karma-babel-preprocessor": "^6.0.1",
    "karma-browserify": "^5.0.1",
    "karma-coverage": "^0.5.5",
    "karma-chrome-launcher": "^0.2.2",
    "karma-jasmine": "^0.3.7",
    "karma-read-json": "^1.1.0"
  },
  "scripts": {
    "test": "karma start"
  },
  "dependencies": {
    "angular": "^v1.5.0",
    "angular-animate": "^1.5.7",
    "angular-route": "^v1.5.0-rc.2",
    "angular-translate": "^2.9.0",
    "angular-ui-bootstrap": "^1.3.3",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "lodash": "^4.14.0",
    "watchify": "^3.7.0"
  }
}


来源:https://stackoverflow.com/questions/38953353/find-a-good-way-to-get-a-coverage-report-with-karma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!