typescript Karma覆盖范围总是空的

kmbjn2e3  于 2023-05-01  发布在  TypeScript
关注(0)|答案(3)|浏览(135)

我一直试图运行因果报应覆盖了几天,现在只找到一个空的空白页如下。

以下是我的配置:

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

};

这是我的业障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);

文件夹结构如下:

你知道我错过了什么吗非常感谢帮助。
谢谢

0tdrvxhp

0tdrvxhp1#

你的karma配置很明显缺少对源代码的引用
请按如下方式修改配置:

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' )
        ],
        [...]
    },
    [...]
}

额外- karma & typescript文件:

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' ]
    },
    [...]
}
wwodge7n

wwodge7n2#

我也遇到了同样的问题,我试过了网上能找到的所有方法,但都没用。所以我用下面的方法解决了这个问题:
1.创建一个新文件夹,或删除项目现有文件夹中的所有内容(如果删除内容,则需要添加、提交和推送所有内容),然后重新克隆同一项目。
1.像往常一样,运行命令“npm install”。
1.然后复制下面的因果报应。conf.js文件内容来替换你自己的因果报应。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'),

这让我很不好受但是因为你一开始就设置错了,即使你改变它也很难把它弄对。
在我做了重新克隆之后,我开始拥有我想要的一切:

祝大家好运

tktrz96b

tktrz96b3#

运行测试时,添加--code-coverage标志。这解决了我的问题。例如:

ng test --watch=false --code-coverage

相关问题