Saturday Jul 26, 2014

Karma Istanbul Code Coverage in NetBeans IDE

Nice job—all your tests are passing. However, it's pretty useful to at least know the percentage of code that's under test. No one's saying that all your code should be under test, just that it's hard to argue against being able to be aware that your 100% test pass rate applies to 20% of your code.

In the JavaScript world, a popular solution in this area is Karma Coverage, which is based on Istanbul, and for which the upcoming NetBeans IDE 8.0.1 has handy integration:

Look at the above screenshot—with one glance you can see the statement in my code that has not been tested.

To get to the above screenshot, I read this article and then checked out this code on GitHub, and then simply opened it in NetBeans IDE, and clicked the menu items that you see above that appear magically on the project when opened into NetBeans IDE, while this is also handy.

Here's the test spec for the above JavaScript file, notice that one test method is commented out. If it were not commented out, the above code coverage would pass 100%.

describe("sqrt", function() {

//  it("should compute the square root of 4 as 2", function() {
//    expect(My.sqrt(4)).toEqual(2);
//  });

  it("should throw an exception if given a negative number", function() {
    expect(function(){ My.sqrt(-1); }).toThrow(new Error("sqrt can't work on negative number"));


For a fullblown NetBeans IDE tutorial on debugging and testing HTML5/JavaScript applications, see this document, though note that it doesn't include code coverage yet, since that's not in the current release of NetBeans IDE:


Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« July 2014 »