角度测试-生成覆盖率报告时出错

时间:2018-09-06 09:41:48

标签: angular typescript code-coverage source-maps istanbul

我正在尝试从$(document).ready(function(){ $('.customer-logos').slick({ slidesToShow: 4, slidesToScroll: 1, autoplay: false, autoplaySpeed: 1500, arrows: false, dots: false, pauseOnHover: false, responsive: [{ breakpoint: 768, settings: { slidesToShow: 4 } }, { breakpoint: 520, settings: { slidesToShow: 3 } }] }); }); const slider = $(".customer-logos"); slider.on('wheel', (function(e) { e.preventDefault(); if (e.originalEvent.deltaY < 0) { $(this).slick('slickNext'); } else { $(this).slick('slickPrev') } })); 获取一份覆盖率报告。没有覆盖,测试运行就没有问题,但是有了这个错误:

ng test --code-coverage

一些调试显示,伊斯坦布尔内部使用的ERROR [karma]: TypeError: Cannot read property 'replace' of undefined at fixPathSeparators (<path>\node_modules\karma-coverage-istanbul-reporter\src\util.js:20:21) at Object.fixWebpackSourcePaths (<path>\node_modules\karma-coverage-istanbul-reporter\src\util.js:41:16) at Object.keys.forEach.filename (<path>\node_modules\karma-coverage-istanbul-reporter\src\reporter.js:46:44) at Array.forEach (<anonymous>) at addCoverage (<path>\node_modules\karma-coverage-istanbul-reporter\src\reporter.js:43:27) at createReport (<path>\node_modules\karma-coverage-istanbul-reporter\src\reporter.js:98:7) at browsers.forEach.browser (<path>\node_modules\karma-coverage-istanbul-reporter\src\reporter.js:213:9) at Array.forEach (<anonymous>) at Collection.forEach (<path>\node_modules\karma\lib\browser_collection.js:93:21) at CoverageIstanbulReporter.onRunComplete (<path>\node_modules\karma-coverage-istanbul-reporter\src\reporter.js:212:16) at Server.<anonymous> (<path>\node_modules\karma\lib\events.js:13:22) at emitTwo (events.js:131:20) at Server.emit (events.js:214:7) at Timeout._onTimeout (<path>\node_modules\karma\lib\executor.js:51:17) at ontimeout (timers.js:498:11) at tryOnTimeout (timers.js:323:5) 没有属性sourceMap。我试图将它添加到几个配置文件中,但是没有运气。

在我的sourceRoot中,我有:

karam.conf.js

非常奇怪的是,我的项目包含一个带有单独测试的库。它们使用相同的配置。

事实

  • 角度6.1.0
  • 茉莉2.99.1
  • 业力1.7.1
  • 因果报导伊斯坦布尔报道:2.0.0

谢谢!

1 个答案:

答案 0 :(得分:0)

解决了!

我有一个自定义库,它是项目的一部分,并与<clib-element></clib-element>一起用于组件中。

在父组件的component.spec.ts中,我这样导入它:

TestBed.configureTestingModule({
  ...
  imports: [ CLibModule ] /* Don't do it like this */
})

相反,我不得不模拟使用的组件。我写了这个模拟组件:

@Component({
  selector: 'clib-element' /* Important: Same selector as real component */
})
export class ElementMockComponent {
  /* declare used methods and attributes */
}

在测试文件中声明它:

TestBed.configureTestingModule({
  ...
  declarations: [ ElementMockComponent ]
})

...错误消失了。希望这对其他人也有帮助!