5
votes

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?

2
Istanbul does not use sourcemaps yet in order to show coverage in TS. But guess it is coming soon. However i chutzpah supports it.PSL
But is there any workaround for that? At the end it is just javascript, so there should be a way to test that kind generated syntax?Marcin
Just as an aside, 100% code coverage does not indicate that the code is well tested and should not be used as the single determining factor as to whether the code being tested has been properly tested.Brocco
That's true. But more branches tested then better is my code quality. If my every file got single branch not testet at all, then it's hard to check quality of code.Marcin
We got same issue here and don't know yet how to solve it. As soon as I find a solution, will post it heredanivicario

2 Answers

1
votes

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

0
votes

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