简体   繁体   中英

How to get 100% of karma branch code coverage in typescript angular app?

I write my angular application with karma and jasmin unit tests. I got code in typescript:

module app {
  ...
}

which generates to javascript like:

var app;
(function (app) {
...
})(app || (app = {}));

Now when I run karma-coverage it shows me that one branch is skipped and it's || (app = {})) ; this one. It happens when I test more files which got app module.

How can I test it in jasmine, to get 100% branch coverage?

If you have a build process using gulp or grunt, after your typescript gets compiled to javascript, you can tell Istanbul (what karma-coverage uses to generate code coverage) to ignore certain lines (like those pesky typescript-generated lines).

You can tell Istanbul to ignore a line by using the /* istanbul ignore next */ comment

(function (app) {
...
})(/* istanbul ignore next */app || (app = {}));

Here's a post explaining how to do just that using gulp.

https://stackoverflow.com/a/33024388/1633757

The remap-istanbul package should be able to convert Istanbul coverage report to original typescript when you compile with sourcemaps.

You may have a look at Code Coverage for Typescript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM