typescript - TypeScript 的 Webpack Karma Istanbul 重映射

标签 typescript code-coverage webpack istanbul

我正在开发客户端应用程序,但在创建正确的 Karma 配置时遇到了问题。现在,我的设置如下:

Webpack:使用 ts-loader,编译 TypeScript,assets 等

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

单元测试都运行良好,但我还没有找到处理 webpack istanbul 重新映射的方法。 Karma-webpacks 似乎没有生成源映射以允许重新映射发生。任何指针将不胜感激!

业力配置:

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
    })
};

最佳答案

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

假设您将输出配置设置为 text: undefined

,此包将在控制台上为您提供 TypeScript 覆盖率输出摘要

将它添加到您现有的工作流中非常简单,有关如何在 github README.md 包中执行此操作的文档。

关于typescript - TypeScript 的 Webpack Karma Istanbul 重映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36108640/

相关文章:

css - 如何使用 chrome dev 协议(protocol)获取 CSS Coverage 数据?

javascript - 在构建阶段检查对象属性的存在

typescript - 三个JS。 "Property ' 类型 'Object3D' 上不存在 Material “使用 getObjectByName 时出错

PHPUnit 覆盖现在不完整,因为类的左大括号没有被覆盖

typescript - 为什么 ts-loader 不编译我的 typescript ?

selenium - PHPUnit_Selenium 代码覆盖率有效吗?

typescript - Webpack typescript 热重载未编译

typescript - 为什么 typescript loader 会导致 html webpack 插件出错?

javascript - 在 JavaScript 中合并并创建唯一区分大小写的数组

angular - 如何避免@types 包引入的补丁级别增加带来重大变化的问题?