业力报道总是空洞的

时间:2016-08-30 16:49:25

标签: unit-testing angular typescript karma-runner karma-coverage

我已经尝试过几天的业力报道,现在却只能找到一个空的空白页面,如下所示。 Karma coverage empty report

这是我的配置:

var path = require('path');

var webpackConfig = require('./webpack.common');

module.exports = function (config) {
  var _config = {
    basePath: '',
    frameworks: ['jasmine'],
    files: [
      { pattern: './karma-shim.js', watched: false }
    ],
    exclude: [],
    preprocessors: {
      './karma-shim.js': ['webpack', 'sourcemap', 'coverage']
    },
    client: {
      captureConsole: false
    },
    webpack: webpackConfig,

    webpackMiddleware: {
      stats: 'errors-only'
    },

    coverageReporter: {
      dir: 'coverage/',
      reporters: [{
        type: 'json',
        dir: 'coverage',
        subdir: 'json',
        file: 'coverage-final.json'
      }]
    },

    remapIstanbulReporter: {
      src: 'coverage/json/coverage-final.json',
      reports: {
        lcovonly: 'coverage/json/lcov.info',
        html: 'coverage/html',
        'text': null
      },
      timeoutNotCreated: 1000, // default value
      timeoutNoMoreFiles: 1000 // default value
    },

    webpackServer: {
      noInfo: true // please don't spam the console when running in karma!
    },
    reporters: ["mocha", "coverage", "karma-remap-istanbul"],
    port: 9876,
    colors: true,
    logLevel: config.LOG_ERROR,
    autoWatch: false,
    browsers: ['PhantomJS'], // you can also use Chrome

    singleRun: true
  };

  config.set(_config);

};

这是我的karma-shim.js档案

Error.stackTraceLimit = Infinity;
require('es6-shim');
require('reflect-metadata');
require('ts-helpers');
require('zone.js/dist/zone');
require('zone.js/dist/long-stack-trace-zone');
require('zone.js/dist/jasmine-patch');
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');

var appContext = require.context('./app', true, /\.spec\.ts/);
appContext.keys().forEach(appContext);

var testing = require('@angular/core/testing');
var browser = require('@angular/platform-browser-dynamic/testing');

testing.setBaseTestProviders(browser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, browser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);

文件夹结构如下: folder structure

知道我在这里缺少什么吗?非常感谢。

由于

2 个答案:

答案 0 :(得分:2)

你的业力配置显然缺少对来源的引用

请修改配置如下:

module.exports = function (config) {
    var _config = {
        [...]
        preprocessors: {
            // Remove coverage as preprocessor for your tests - 
            // Istambul (runs coverage behind the scene for karma) will 
            // instrumentate this
            './karma-shim.js': ['webpack', 'sourcemap'],
            // Add path to your source (.js or .ts - depands on your project)
            './index.ts': [ 'coverage' ]
        },
        [...]
    },
    [...]
}

说明:覆盖率会针对您的单元测试测试您的代码 - 您需要为代码提供入口点以获得Karma分析覆盖率。

附加内容 - 添加karma-coverage插件:

module.exports = function (config) {
    var _config = {
        [...]
        plugins: [
            require( 'karma-coverage' )
        ],
        [...]
    },
    [...]
}

额外 - 业力&打字稿文件:

module.exports = function (config) {
    var _config = {
        [...]
        plugins: [
            require( '@angular/cli/plugins/karma' )
        ],
        [...]
    },
    [...]
    preprocessors: {
        './src/test.ts': [ '@angular/cli' ],
        './index.ts': [ 'coverage' ]
    },
    [...]
    mime: {
       'text/x-typescript': [ 'ts', 'tsx' ]
    },
    [...]
}

答案 1 :(得分:0)

我遇到了同样的问题,并且尝试了所有可以在Internet上找到的东西,但是没有任何效果。因此,我以以下困难但非常简单的方式解决了该问题:

  1. 创建一个新文件夹,或删除项目中现有文件夹的所有内容(如果您删除了该内容,则需要添加,提交和进行所有操作),然后再次克隆相同的项目。

  2. 像往常一样,运行命令“ npm install”。

  3. 然后复制以下karma.conf.js文件内容以替换您自己的karma.conf.js文件内容:

    createRequire

很可能是'coverage'部分的错误

 module.exports = function (config) {
   config.set({
     basePath: '',
     frameworks: ['jasmine', '@angular-devkit/build-angular'],
     plugins: [
       require('karma-jasmine'),
       require('karma-chrome-launcher'),
       require('karma-jasmine-html-reporter'),
       require('karma-coverage-istanbul-reporter'),
       require('@angular-devkit/build-angular/plugins/karma')
     ],
     client: {
       clearContext: false // leave Jasmine Spec Runner output visible in browser
     },
     coverageIstanbulReporter: {
       dir: require('path').join(__dirname, 'coverage'),
       reports: ['html', 'lcovonly', 'text-summary'],
       fixWebpackSourcePaths: true
     },
     reporters: ['progress', 'kjhtml'],
     port: 9876,
     colors: true,
     logLevel: config.LOG_INFO,
     autoWatch: true,
     browsers: ['Chrome'],
     singleRun: false,
     restartOnFileChange: false,
   });
 };

就我而言,原始设置是

    dir: require('path').join(__dirname, 'coverage'), 

这让我很难受。但是由于您在一开始就将其设置为错误,因此即使您进行更改,也很难做到正确。

重新克隆后,我开始拥有所需的一切: enter image description here

祝大家好运。