12

几天来,我一直在尝试运行业力覆盖,结果却发现如下所示的空白页。 业力覆盖空报告

这是我的配置:

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

文件夹结构如下: 文件夹结构

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

谢谢

4

2 回答 2

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' ]
    },
    [...]
}
于 2017-12-30T00:35:23.163 回答
0

我遇到了同样的问题,我已经尝试了在互联网上可以找到的所有内容,但没有任何效果。所以我用以下困难但非常简单的方式解决了它:

  1. 创建一个新文件夹,或删除项目现有文件夹的所有内容(如果删除内容,您需要添加、提交和推送所有内容),然后重新克隆同一个项目。

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

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

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

就我而言,原始设置是

    dir: require('path').join(__dirname, './../coverage/360-app'),

这让我很难受。但是因为你一开始就设置错了,即使你改变它也很难把它弄好。

重新克隆后,我开始拥有我想要的一切: 在此处输入图像描述

祝大家好运。

于 2020-07-14T21:00:06.310 回答