istanbul icon indicating copy to clipboard operation
istanbul copied to clipboard

Odd branch coverage issues with generator functions

Open cdutson opened this issue 8 years ago • 8 comments

Hi there! So I've been using istanbul for code coverage. Big fan. The project I've been using it on is a React/Redux app that utilizes Redux-Sagas. The big thing with sagas is it's use of generator functions. I've found that there's some ... weird branch-coverage issues when it comes to branch coverage. I've attached a couple screenshots to demonstrate.

screen shot 2016-06-13 at 8 47 37 pm screen shot 2016-06-13 at 8 44 33 pm screen shot 2016-06-13 at 8 44 42 pm

Not really sure what's going on with this. I'm not too concerned, as I know I've covered the code. Would just be nice to see those 100% across the board

cdutson avatar Jun 14 '16 00:06 cdutson

Same for me, duplicate of https://github.com/gotwarlost/istanbul/issues/248

GuillaumeCisco avatar Jun 14 '16 13:06 GuillaumeCisco

Is there a workaround possible with /* istanbul ignore XX */? I tried if because in the HTML report is shows this 'I' symbol, but nothing changes. When I use next the whole function is ignored :(

2016-06-15-184045_759x545_scrot

hoschi avatar Jun 15 '16 16:06 hoschi

I presume you are transpiling this code? I think what is going on is that the transpiler is putting in a conditional in front of the generator function causing the ignored branch.

gotwarlost avatar Jun 19 '16 17:06 gotwarlost

Right, I use Babel.

Krishnan Anantheswaran [email protected] schrieb am So., 19. Juni 2016 19:31:

I presume you are transpiling this code? I think what is going on is that the transpiler is putting in a conditional in front of the generator function causing the ignored branch.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/gotwarlost/istanbul/issues/645#issuecomment-227009619, or mute the thread https://github.com/notifications/unsubscribe/AAJ9OD9r4WWrcahhqlk0JG5UZtY8VBP4ks5qNXzrgaJpZM4I03dM .

hoschi avatar Jun 19 '16 18:06 hoschi

:+1:

michaelgilley avatar Jul 26 '16 20:07 michaelgilley

I was confused by the happy result with no real solution other than pointing the finger at Babel.

I, too, was having this issue with a project I am working on with React/Redux/Sagas. This post sent me looking for a way to skip the coverage on the 'extraneous' if just to find out that both Babel and Istanbul were correctly reporting coverage from my tests.

Poking with a stick, /* istanbul ignore if */, didn't seem to help other than making me think harder. I read this article on generators.

given:

    function* dataConsumer() {
        console.log('Started');
        console.log(`1. ${yield}`); // (A)
        console.log(`2. ${yield}`);
        return 'result';
    }

behavior:

    > let genObj = dataConsumer();
    > genObj.next()
    Started
    { value: undefined, done: false }

    > genObj.next('a')
    1. a
    { value: undefined, done: false }

    > genObj.next('b')
    2. b
    { value: 'result', done: true }

The underlying result is a branch condition indicating exit with the done property equal to true. The missing test, in my case, was to call the final next and receive an object with done: true (This can be extra troublesome in the cases where working with Sagas you might have a sequence of events wrapped in a while(true) statement. (Following examples from the interwebs doesn't always give you testable code ;-) ))

Hope this is helpful

gdw-atwork avatar Aug 28 '16 21:08 gdw-atwork

Is there any update to this issue?

rosesonfire avatar Mar 26 '18 15:03 rosesonfire

@gdw-atwork nice catch man! You saved my life :)

SuEric avatar Feb 05 '19 23:02 SuEric