gotwarlost / istanbul

Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale.
Other
8.7k stars 786 forks source link

Istanbul function coverage for arrow function? #486

Open sweetim opened 8 years ago

sweetim commented 8 years ago

I have a module that I would like to have code coverage using Istanbul, but I cant get it working when I use arrow function.

my module compute.js

'use strict';

exports.addition = (a, b) => {
    return a + b;
}

exports.multiplication = (a, b) => {
    return a * b;
}

and my unit test code test.js

describe('Compute', function() {
    describe('addition()', function() {
        it('should add', function() {
            assert.equal(5, compute.addition(2, 3))
            assert.equal(15, compute.addition(2, 13))
        })
    })

    describe('multiplication()', function() {
        it('should multiply', function() {
            assert.equal(6, compute.multiplication(2, 3))
            assert.equal(26, compute.multiplication(2, 13))
        })
    })
})

When i run this command

node_modules/.bin/istanbul cover node_modules/mocha/bin/_mocha test/test.js

I get my coverage summary shown below:

================== Coverage summary =======================
Statements : 100% (4/4)
Branches   : 100% (0/0)
Functions  : 100% (0/0)
Lines      : 100% (4/4)

In my Function row, i am getting 0/0, if i change my compute.js to use function call.

exports.addition = function(a, b) {
    return a + b;
}

exports.multiplication = function(a, b) {
    return a * b;
}

now, i am getting the correct code coverage

================== Coverage summary =======================
Statements : 100% (4/4)
Branches   : 100% (0/0)
Functions  : 100% (2/2)
Lines      : 100% (4/4)

package.json

"istanbul": "^0.4.0",
"mocha": "^2.3.4"

May i know why the arrow function is not working and how can I fix them?

gotwarlost commented 8 years ago

Looks like istanbul is not counting arrow functions as "functions". This should be easy to fix but I'm in he middle of a refactor so I can only get to it later.

Thanks for reporting this issue.

olegdeezus commented 8 years ago

@sweetim I had same error. But in my case root of the problem was damn stupid. I just forgot to remove describe.only from one of my tests! =))) I thought it may help you.

simonrenoult commented 8 years ago

The issue still exists with istanbul@0.4.5. Any update?

brandonsturgeon commented 7 years ago

Istanbul's inability to cover arrow functions is causing me to do some frustrating refactoring. Do we have any updates on this?

sweetim commented 7 years ago

You could use nyc package https://www.npmjs.com/package/nyc this problem has been resolved with that package! ;)

dannygoncalves commented 6 years ago

@sweetim it should be a native function from Istanbul instead using another package.

cross19xx commented 4 years ago

We seem to be having this same problem with Jest here

vic1707 commented 2 years ago

Any updates?

alvaropsouza commented 2 years ago

Updates on this issue?

lorenzogrv commented 2 years ago

Still active

bpaczkowski commented 2 years ago

As stated in the readme, this version of Istanbul has been deprecated, 5 years ago already. Use https://github.com/istanbuljs/nyc @gotwarlost why don't you put this repo into read-only mode?

lorenzogrv commented 2 years ago

As stated in the readme, this version of Istanbul has been deprecated, 5 years ago already. Use https://github.com/istanbuljs/nyc @gotwarlost why don't you put this repo into read-only mode?

Sorry!