Code Coverage report shown only for polyfills.ts and test.ts under Dev\ folder. Doest show coverage for files under src folder

你说的曾经没有我的故事 提交于 2020-06-09 07:08:06

问题


In my angular 7 app, when i run "ng test --watch=false --code-coverage true" i get the coverage report in my coverage folder, but coverage is shown only for two files. (Polyfills.ts & test.ts). Below is my project structure. Please let me know how to get coverage for files under SRC folder.

Note: when i run ng test all the tests under SRC folder is getting executed.

dev
--app
--test.ts
--tsconfig.app.json
--tsconfig.spec.json
src
--lib
  --component
  --services
angular.json
karma.conf.json

test.ts

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('../src', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
// Finally, start Karma to run the tests.
__karma__.start();

tsconfig.spec.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "module": "commonjs",
    "baseUrl": "",
    "types": [
      "jasmine",
      "node"
    ],
    "typeRoots": [
      "../node_modules/@types"
    ]
  },
  "files": [
    "test.ts",
    "polyfills.ts"
  ],
  "include": [
    "../src/**/*.spec.ts"
  ]
}

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-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('karma-trx-reporter'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    mime: {
      'text/x-typescript': ['ts', 'tsx']
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, 'coverage'),
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true
    },
    trxReporter: {
      outputFile: 'test/test-results.trx',
      shortTestName: false
    },

    reporters: config.angularCli && config.angularCli.codeCoverage ?
      ['progress', 'coverage-istanbul', 'trx'] :
      ['progress', 'kjhtml', 'trx'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

angular.json

"test": {
  "builder": "@angular-devkit/build-angular:karma",
  "options": {
    "main": "dev/test.ts",
    "codeCoverage": true,
    "karmaConfig": "./karma.conf.js",
    "polyfills": "dev/polyfills.ts",
    "tsConfig": "dev/tsconfig.spec.json",
    "scripts": [],
    "styles": [
      "dev/styles.scss"
    ],
    "assets": [
      "dev/assets",
      {
        "glob": "**/*",
        "input": "node_modules/@honda/shared-module/assets/img",
        "output": "/assets/img"
      },
      "dev/assets",
      {
        "glob": "**/*",
        "input": "src/lib/config",
        "output": "/assets/config"
      },
      "dev/manifest.json"
    ]
  }
}

来源:https://stackoverflow.com/questions/62152730/code-coverage-report-shown-only-for-polyfills-ts-and-test-ts-under-dev-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!