karma-browserify coverage reports contain file include paths instead of source code

和自甴很熟 提交于 2019-12-12 03:02:08

问题


Using karma-browserify to do unit tests with Jasmine. The tests correctly run but the coverage reports show file include paths instead of source code. You can reproduce this by installing the following project and run 'gulp unit':

https://github.com/bshack/shackstack

Here is an example of the coverage report contents:

typeof require === "function" && require("/xxx/xxx/xxx/shackstack/app/media/script/service/utilities.js");

Here is my karma.config:

module.exports = function(karma) {
    'use strict';
    karma.set({
        basePath: '',
        frameworks: [
            'jasmine',
            'browserify'
        ],
        files: [{
            pattern: 'app/media/script/service/*.js',
            included: true
        },
        {
            pattern: 'app/media/test/spec/*Spec.js',
            included: true

        }],
        reporters: [
            'progress',
            'coverage'
        ],
        preprocessors: {
            'app/media/script/service/*.js': [
                'browserify',
                'coverage'
            ],
            'app/media/test/spec/*Spec.js': [
                'browserify'
            ]
        },
        browsers: [
            //'Chrome',
            //'Firefox',
            //'Safari',
            'PhantomJS'
        ],
        singleRun: false,
        autoWatch: false,
        // browserify configuration
        browserify: {
            debug: true,
            transform: [
                'brfs',
                'browserify-shim'
            ]
        },
        coverageReporter: {
            type: 'html',
            dir: 'app/report/istanbul/',
            subdir: '.'
        },
        // If browser does not capture in given timeout [ms], kill it
        captureTimeout: 60000
    });
};

any thoughts?


回答1:


Fixed, basically you don't use karma coverage as you would normally, instead you have to use istanbul as a browserify transform.

var istanbul = require('browserify-istanbul');
module.exports = function(karma) {
    'use strict';
    karma.set({
        basePath: '',
        frameworks: [
            'jasmine',
            'browserify'
        ],
        files: [{
            pattern: 'app/media/script/service/*.js'
        },
        {
            pattern: 'app/media/test/spec/*Spec.js'
        }],
        reporters: [
            'progress',
            'coverage'
        ],
        preprocessors: {
            'app/media/script/service/*.js': [
                'browserify'
            ],
            'app/media/test/spec/*Spec.js': [
                'browserify'
            ]
        },
        browsers: [
            //'Chrome',
            //'Firefox',
            //'Safari',
            'PhantomJS'
        ],
        singleRun: false,
        autoWatch: false,
        browserify: {
            debug: true,
            transform: [
                'brfs',
                'browserify-shim',
                istanbul({
                    ignore: ['**/node_modules/**']
                })
            ]
        },
        coverageReporter: {
            type: 'html',
            dir: 'app/report/istanbul/',
            subdir: '.'
        },
        // If browser does not capture in given timeout [ms], kill it
        captureTimeout: 60000
    });
};


来源:https://stackoverflow.com/questions/35122592/karma-browserify-coverage-reports-contain-file-include-paths-instead-of-source-c

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