Angular中伊斯坦布尔覆盖报告中的标记异常

Posted

技术标签:

【中文标题】Angular中伊斯坦布尔覆盖报告中的标记异常【英文标题】:marker abnormal in istanbul coverage report in Angular 【发布时间】:2019-02-01 20:15:24 【问题描述】:

我使用 Karma 运行单元测试并生成覆盖率报告,但是,彩色部分(如这些红色代码)似乎异常移动,如下所示:

相关库版本为: "@angular/core": "^6.1.0", "@angular/cli": "^6.1.1", "@angular/compiler-cli": "^6.1.0", “茉莉花核”:“~3.1.0”, "jasmine-spec-reporter": "~4.2.1", “业力”:“~2.0.5”, “karma-chrome-launcher”:“~2.2.0”, “业力-cli”:“~1.0.1”, “业力覆盖-伊斯坦布尔记者”:“^2.0.1”, “业力茉莉花”:“~1.1.0”, "karma-jasmine-html-reporter": "^1.2.0",

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

package.json


  "name": "rubik-frontend",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": 
    "ng": "ng",
    "prestart": "npm run i18n",
    "start": "ng serve --proxy-config proxy.conf.json",
    "prebuild": "npm run i18n",
    "build": "ng build",
    "build:prod": "ng build --prod --aot",
    "prebuild:prod": "npm run i18n",
    "icon": "node scripts/generateIconSymbol",
    "i18n": "node scripts/compileI18n",
    "test": "ng test rubik-frontend --code-coverage",
    "test:single": "ng test rubik-frontend --watch=false",
    "lint": "ng lint",
    "lint:fix": "ng lint --fix --type-check",
    "e2e": "ng e2e"
  ,
  "private": true,
  "dependencies": 
    "@angular/animations": "^6.1.0",
    "@angular/cdk": "^6.4.1",
    "@angular/common": "^6.1.0",
    "@angular/compiler": "^6.1.0",
    "@angular/core": "^6.1.0",
    "@angular/forms": "^6.1.0",
    "@angular/http": "^6.1.0",
    "@angular/platform-browser": "^6.1.0",
    "@angular/platform-browser-dynamic": "^6.1.0",
    "@angular/router": "^6.1.0",
    "cheerio": "^1.0.0-rc.2",
    "core-js": "^2.5.5",
    "dom-to-image": "^2.6.0",
    "echarts": "^4.0.4",
    "jsplumb": "^2.7.3",
    "lodash-es": "^4.17.8",
    "ng2-dnd": "^5.0.2",
    "normalize.css": "^8.0.0",
    "path": "^0.12.7",
    "rxjs": "^6.2.2",
    "studio-ui": "^0.2.10",
    "zone.js": "^0.8.26"
  ,
  "devDependencies": 
    "@angular-devkit/build-angular": "~0.7.0",
    "@angular/cli": "^6.1.1",
    "@angular/compiler-cli": "^6.1.0",
    "@angular/language-service": "^6.1.0",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "^10.5.4",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~3.1.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~2.0.5",
    "karma-chrome-launcher": "~2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^2.0.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^1.2.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~2.9.2"
  

我使用的命令行是: ng test rubik-frontend --code-coverage

谢谢!

【问题讨论】:

你到底在运行什么命令?请发布您的 test.ts 以及您的 package.json 脚本。我记得通过删除我的测试命令的--source-map false 解决了这个问题。当您的测试遇到错误或异常时,也会发生这种情况。 @trichetriche 感谢您的回复。我已经更新了我的帖子。 【参考方案1】:

请尝试使用

ng test rubik-frontend --code-coverage true --source-map true

据我所知,这解决了我的问题。我不确定,为什么它默认不使用源映射。

【讨论】:

我遇到了同样的问题,将--source-map=true 添加到命令中即可解决。不知道为什么默认情况下不启用。

以上是关于Angular中伊斯坦布尔覆盖报告中的标记异常的主要内容,如果未能解决你的问题,请参考以下文章

Angular 9 - 生成空的伊斯坦布尔测试覆盖率报告

伊斯坦布尔覆盖 HTML 报告中的嵌套目录

Angular CLI - 获取包含所有来源的覆盖率报告

Angular 单元测试中业力代码覆盖率报告中的 1x 3x 等是啥意思?

如何从代码覆盖率伊斯坦布尔记者中排除模拟文件

为在 phantomjs 中的 browserify 包上运行的 jasmine 测试生成伊斯坦布尔代码覆盖率报告(通过 grunt)