Flo
Flo

Reputation: 45

Angular testing - Error generating coverage report

I'm trying to get a coverage report from ng test --code-coverage. Without coverage, the tests are running without problems, but with it I get this error:

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)

Some debugging showed that the sourceMap used inside Istanbul has not attribute sourceRoot. I tried to add this to several config files with no luck.

In my karam.conf.js I have:

coverageIstanbulReporter: {
  dir: require('path').join(__dirname, '../coverage'),
  reports: ['html', 'lcovonly'],
  fixWebpackSourcePaths: true
}

Very strange is the fact that my project contains a library with separate tests. They work with an identical configuration.

Facts:

Thanks!

Upvotes: 0

Views: 2159

Answers (1)

Flo
Flo

Reputation: 45

Solved it!

I have a custom library which ist part of the project and is used in a component with <clib-element></clib-element>.

In the component.spec.ts of the parent component I imported it like this:

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

Instead, I had to mock the used component. I wrote this mock component:

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

Declare it in the test file:

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

... and the error is gone. Hope this helps others, too!

Upvotes: 0

Related Questions