How to specify which functions/methods should be covered by a test, using Karma, Jasmine, and Istanbul

I am trying to figure out how to restrict my tests, so that the coverage reporter only considers a function covered when a test was written specifically for that function.

The following example from the PHPUnit doc shows pretty good what I try to achieve:

The @covers annotation can be used in the test code to specify which method(s) a test method wants to test:

 * @covers BankAccount::getBalance
public function testBalanceIsInitiallyZero()
    $this->assertEquals(0, $this->ba->getBalance());

If the test above would be executed, only the function getBalance will be marked as covered, and none other.

Now some actual code sample from my JavaScript tests. This test shows the unwanted behaviour that I try to get rid of:

it('Test get date range', function()
    expect(dateService.getDateRange('2001-01-01', '2001-01-07')).toEqual(7);

This test will mark the function getDateRange as covered, but also any other function that is called from inside getDateRange. Because of this quirk the actual code coverage for my project is probably a lot lower than the reported code coverage.

How can I stop this behaviour? Is there a way to make Karma/Jasmine/Istanbul behave the way I want it, or do I need to switch to another framework for JavaScript testing?


I don't see any particular reason for what you're asking. I'd say if your test causes a nested function to be called, then the function is covered too. You are indeed indirectly testing that piece of code, so why shouldn't that be included in the code coverage metrics? If the inner function contains a bug, your test could catch it even if it's not testing that directly.

You can annotate your code with special comments to tell Istanbul to ignore certain paths: but that's more for the opposite I think, not to decrease coverage if you know you don't want a particular execution path to be covered, maybe because it would be too hard to write a test case for it.

Also, if you care about your "low level" functions tested in isolation, then make sure your code is structured in a modular way so that you can test those by themselves first. You can also set up different test run configurations, so you can have a suite that tests only the basic logic and reports the coverage for that.

As suggested in the comments, mocking and dependency injections can help to make your tests more focused, but you basically always want to have some high level tests where you check the integrations of these parts together. If you mock everything then you never test the actual pieces working together.

Need Your Help

Difference between paste and print (affecting result of function)


To start off, I'm not really sure what the difference between paste and print is. But I am using "print" to spit out generic statements and "paste" to spit out statements that use/ reference specific

Meteor + React + Highchart

meteor highcharts reactjs npm

I'm using Meteor 1.3(beta.5) and trying to use react optimized npm highchart package called kirjs/react-highcharts (