Webpack Karma Istanbul为TypeScript重新映射

cal*_*via 11 code-coverage typescript istanbul webpack

我正在开发一个客户端应用程序,我在创建正确的Karma配置时遇到了麻烦.现在,我的设置如下:

Webpack:使用ts-loader,编译TypeScript,资产等.

Karma:使用webpack插件,加载Webpack配置(使用ts-loader),然后使用Jasmine + PhantomJS运行所有单元测试

单元测试所有运行正常,但我还没有找到一种方法来处理webpack istanbul重映射.Karma-webpacks似乎没有生成源映射以允许重映射发生.任何指针将不胜感激!

Karma配置:

var webpackConfig = require("./webpack.config.js");
delete webpackConfig.entry;

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'],

        // list of files / patterns to load in the browser
        files: [
            // Non-automatically bundled libraries
            'app/client/js/lib/easeljs.min.js',
            'app/client/js/lib/tweenjs.min.js',
            // Entry File
            'app/client/js/index.ts',
            'app/client/html/**/*.html',

            // Test files and dependencies
            'node_modules/angular-mocks/angular-mocks.js',
            'test/client/**/*.spec.js'
        ],

        // preprocess matching files before serving them to the browser
        // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
        preprocessors: {
            '**/*.html': ['ng-html2js'],
            'app/client/js/index.ts': ['webpack', 'sourcemap', 'coverage']
        },

        ngHtml2JsPreprocessor: {
            cacheIdFromPath: function (filepath) {
                // Remaps the path for Karma webpack
                return '/_karma_webpack_//' + filepath.replace(/^.*[\\\/]/, '');
            },
            moduleName: 'templates'
        },

        webpack: webpackConfig,

        webpackMiddleware: {
            noInfo: true
        },

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

        coverageReporter: {
            dir: 'build/client/test/coverage/',
            reporters: [
                {
                    type: 'json',
                    subdir: '.'
                }
            ]
        },

        // 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: ['PhantomJS'],

        // Concurrency level
        // how many browser should be started simultaneously
        concurrency: Infinity
    })
};
Run Code Online (Sandbox Code Playgroud)

小智 2

目前Karma Remap Istanbul是唯一能够内联生成 TypeScript 覆盖范围的软件包。remap-istanbul显然,它也可以通过简单地调用生成的coverage.json来管理。

假设您将输出配置设置为,此包将在控制台上为您提供 TypeScript 覆盖率输出摘要text: undefined

将其添加到现有工作流程中非常简单,有关如何执行此操作的文档位于 github 包中README.md