【问题标题】:Find a good way to get a coverage report with karma?找到一种获得业力覆盖率报告的好方法?
【发布时间】:2016-12-21 13:21:48
【问题描述】:

我正在使用 karma-coverage + browserify + angular。我从覆盖中得到的结果是 100% 的被测文件都被覆盖了,这是不正确的。知道如何完成这项工作吗?

这是我的文件 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
    })
}

这是我的 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"
          ]
        }
      ]
    ]
  }
}

【问题讨论】:

  • 感谢链接,但我使用了 babelify + browserify + angular,我不知道我可以在 karma.conf 文件中添加什么配置,因为它给了我 100% 的结果(覆盖率) .
  • 您可能需要关注this question - 从您的标题来看,这似乎也是您所要求的。
  • 但我使用了 babelify,我搜索了可以添加到 karam.conf 的配置以进行 browserify。
  • 我喜欢它,browserify: { debug: true, transform: [ ['babelify',{ presets: ['es2015'] }] ] },但问题总是显示 100% ?? ?

标签: angularjs karma-runner code-coverage browserify


【解决方案1】:

感谢大家,我找到了正确的解决方案。

这是我的文件 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"
  }
}

【讨论】:

  • 请说明您必须更改哪些内容才能使其正常工作。代码转储不是特别有用。
  • 这是配置浏览器的问题,我在我的文件 karma.conf 中添加了这个: let babelify = require( 'babelify' );让 browserify = require('browserify');让 browserifyBabalIstanbul = require('browserify-babel-istanbul');让 isparta = require('isparta');
  • 还有这个 : browserify: { debug: true, transform: [ browserifyBabalIstanbul({ instrumenter: isparta, instrumenterConfig: { babel: { presets: ["es2015"] } }, ignore: ['/node_modules/', '/unitest/'] }), [ babelify, { presets: ["es2015"] } ] },
猜你喜欢
  • 2016-10-25
  • 1970-01-01
  • 2016-03-21
  • 2021-10-19
  • 1970-01-01
  • 2022-07-27
  • 1970-01-01
  • 1970-01-01
  • 2015-12-27
相关资源
最近更新 更多