Istanbul: Istanbul function coverage for arrow function?

Created on 18 Nov 2015  路  7Comments  路  Source: gotwarlost/istanbul

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?

bug v1

Most helpful comment

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

All 7 comments

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.

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

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

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

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

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

We seem to be having this same problem with Jest here

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jasonpincin picture jasonpincin  路  22Comments

briancullinan picture briancullinan  路  22Comments

NiGhTTraX picture NiGhTTraX  路  36Comments

ouhouhsami picture ouhouhsami  路  16Comments

lizhexia picture lizhexia  路  60Comments