简体   繁体   English

Angular 未生成业力代码覆盖率报告文件夹

[英]Angular karma code coverage report folder not generated

When I run ng test --code-coverage , The coverage report is sometimes not generating, sometimes it is generating so I'm unable to verify the coverage statement after doing test suites.当我运行ng test --code-coverage时,覆盖率报告有时不会生成,有时会生成,所以我无法在完成测试套件后验证覆盖率声明。

My Karma configuration我的业力配置

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

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'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

In my case everything seemed correct, but no coverage files or messages were generated on the console.就我而言,一切似乎都正确,但控制台上没有生成覆盖文件或消息。

What solved the problem was editing angular.json, adding the following key:解决问题的是编辑angular.json,添加以下键:

projects.ClientApp.test.options.codeCoverage = true;

The reason, your code coverage is not generating is that, there are some broken unit tests which stops the generation of the code coverage folder.您的代码覆盖率未生成的原因是,有一些损坏的单元测试会停止生成代码覆盖率文件夹。 Even though, it is generates some times, but that also must be taking much time then usual.虽然,它会产生一些时间,但这也必须比平常花费更多的时间。 Follow the below steps, in order to identify and fix the unit tests so that, code-coverage file will generate each time-按照以下步骤,为了识别和修复单元测试,每次都会生成代码覆盖文件-

  1. Run the tests using npm test .使用npm test
  2. Open the browser and check the tests, there shouldn't be any broken or failed tests case.打开浏览器并检查测试,不应该有任何损坏或失败的测试用例。
  3. It shouldn't give any console error (Verify on console and fix it.)它不应该给出任何控制台错误(在控制台上验证并修复它。)
  4. Verify that all the unit tests are running and passing successfully.验证所有单元测试是否正在运行并成功通过。 Sometimes it shows passed unit tests, but it gives some popup errors or console errors, so these tests prevents the unit test code coverage generation.有时它显示通过的单元测试,但它会给出一些弹出错误或控制台错误,因此这些测试阻止了单元测试代码覆盖率的生成。

Fix all the tests and it should generate the unit tests code coverage folder everytime.修复所有测试,它应该每次都生成单元测试代码覆盖率文件夹。

通过以下命令生成新的代码覆盖率文件夹

node --max_old_space_size=4096 ./node_modules/karma/bin/karma start ./test-config/karma.conf.js --coverage

Add the following config in your Karma.conf.js file在您的 Karma.conf.js 文件中添加以下配置

module.exports = function(config) {
  config.set({
   ...
   coverageReporter: {
      dir: require('path').join(__dirname, 'dist/coverage/app-coverage'),
      subdir: '.',
      reporters: [
        { type: 'html' },
        { type: 'text-summary' },
        { type: 'cobertura' }
      ]
    }
   }

and then run:然后运行:

ng test  --code-coverage=true

It will generate precise code coverage report, now go to the directory dist/coverage/app-coverage and open index.html file in browser.它将生成精确的代码覆盖率报告,现在 go 到目录dist/coverage/app-coverage并在浏览器中打开 index.html 文件。 It will provide code coverage for individual files and which code section is need to cover.它将提供单个文件的代码覆盖率以及需要覆盖的代码部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Karma,伊斯坦布尔 - 代码覆盖率报告 Unknown% (0/0) - Karma , Istanbul - code coverage report Unknown% ( 0/0 ) Karma 提供了错误的代码覆盖率报告 - Karma delivers wrong code coverage report Angular 9:业力覆盖率:如何仅显示特定文件的覆盖率报告 - Angular 9 : Karma Coverage : How to display coverage report with only specific files Angular 6 - Sonarqube 覆盖率报告始终为 0 但 karma 显示覆盖率 - Angular 6 - Sonarqube coverage report is always 0 but karma shows coverage 1x 3x等在角度单元测试中的业力代码覆盖率报告中意味着什么? - what does 1x 3x etc mean in karma code coverage report in Angular Unit testing? 使用Karma,Angular和Husky执行代码覆盖 - Code Coverage Enforcement with Karma, Angular and Husky Angular 2 + Karma + karma-jspm + karma-coverage +打字稿报告phantomJS失败 - Angular 2 + Karma + karma-jspm + karma-coverage + typescript report phantomJS failure 未创建角度4代码覆盖率文件夹 - angular 4 code coverage folder not created Angular2 Karma Code Coverage仅显示模型,模块和服务 - Angular2 Karma Code Coverage only showing models, modules and services 使用 Angular 和 Jasmine/Karma 使用私有方法进行测试和代码覆盖 - Tests and code-coverage with private methods with Angular and Jasmine/Karma
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM