istanbul icon indicating copy to clipboard operation
istanbul copied to clipboard

Istanbul function coverage for arrow function?

Open sweetim opened this issue 8 years ago • 12 comments

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?

sweetim avatar Nov 18 '15 00:11 sweetim

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.

gotwarlost avatar Nov 21 '15 00:11 gotwarlost

@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.

olegdeezus avatar Jun 15 '16 14:06 olegdeezus

The issue still exists with [email protected]. Any update?

simonrenoult avatar Oct 14 '16 16:10 simonrenoult

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

brandonsturgeon avatar Mar 02 '17 22:03 brandonsturgeon

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

sweetim avatar Mar 03 '17 12:03 sweetim

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

dannygoncalves avatar Feb 26 '18 21:02 dannygoncalves

We seem to be having this same problem with Jest here

cross19xx avatar Feb 22 '20 21:02 cross19xx

Any updates?

vic1707 avatar Nov 09 '21 20:11 vic1707

Updates on this issue?

alvaropsouza avatar Nov 29 '21 21:11 alvaropsouza

Still active

lorenzogrv avatar Oct 27 '22 10:10 lorenzogrv

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?

bpaczkowski avatar Oct 27 '22 10:10 bpaczkowski

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!

lorenzogrv avatar Oct 27 '22 23:10 lorenzogrv