Angular 4:如何为所有文件生成代码覆盖率并为 Sonar 导出

fir*_*baa 6 code-coverage typescript sonarqube karma-jasmine angular

我正在为我的 angular 应用程序实现代码覆盖率 taaks,以在 Sonarqube 中显示其结果。

我的karma.conf.js看起来像这样:

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

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular/cli'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular/cli/plugins/karma')
    ],
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    files: [
      { pattern: './src/test.ts', watched: false }
    ],
    preprocessors: {
      './src/test.ts': ['@angular/cli']
    },
    mime: {
      'text/x-typescript': ['ts','tsx']
    },
    coverageIstanbulReporter: {
      reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true
    },
    angularCli: {
      environment: 'dev'
    },
    reporters: config.angularCli && config.angularCli.codeCoverage
              ? ['progress', 'coverage-istanbul']
              : ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};
Run Code Online (Sandbox Code Playgroud)

我的test.ts文件如下所示:

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';

// Unfortunately there's no typing for the `__karma__` variable. Just declare it as any.
declare var __karma__: any;
declare var require: any;

// Prevent Karma from running prematurely.
__karma__.loaded = function () {};

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /app.component\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
// Finally, start Karma to run the tests.
__karma__.start();
Run Code Online (Sandbox Code Playgroud)

当我运行一些测试时,我执行: ng tests --code-coverage

1 - 我的第一个问题:事实上,它只显示了我的代码的一部分/少数部分 而不是我的所有文件的覆盖任务的结果

那么如何为我的所有项目文件生成覆盖率

2 -我的第二个问题是声纳:

事实上,在业力中产生的结果似乎与声纳中的表现不同。

示例:业力中是 50% ->声纳中显示 10%

我的sonar-project.properties看起来像这样:

# must be unique in a given SonarQube instance
sonar.projectKey=Nomadis
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=SocleRCD_FrontV2
sonar.projectVersion=2.0

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set. 
sonar.sources=./src/app

sonar.exclusions=node_modules/**

# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

sonar.tests=./src/app
#sonar.test.inclusions=**/*.spec.ts

sonar.ts.tslintconfigpath=./tslint.json


# To import the LCOV report
sonar.typescript.lcov.reportPaths=coverage/lcov.info
Run Code Online (Sandbox Code Playgroud)

有什么建议 ??