Istanbul: Istanbul removes function name in coverage

Created on 20 Sep 2016  路  6Comments  路  Source: gotwarlost/istanbul

This:

function square (x) { return x * x; }

Becomes this:

function (args) { var output;

var __cov_LQgId1OfxIM2Vvj2j1AaeQ = (Function('return this'))();

if (!__cov_LQgId1OfxIM2Vvj2j1AaeQ.$$coverage$$) { __cov_LQgId1OfxIM2Vvj2j1AaeQ.$$coverage$$ = {}; }
__cov_LQgId1OfxIM2Vvj2j1AaeQ = __cov_LQgId1OfxIM2Vvj2j1AaeQ.$$coverage$$;
if (!(__cov_LQgId1OfxIM2Vvj2j1AaeQ['.../istanbul/test/instrumentation/test-es6-arrow-fn.js'])) {
   __cov_LQgId1OfxIM2Vvj2j1AaeQ['.../istanbul/test/instrumentation/test-es6-arrow-fn.js'] = {"path":".../istanbul/test/instrumentation/test-es6-arrow-fn.js","s":{"1":1,"2":0},"b":{},"f":{"1":0},"fnMap":{"
1":{"name":"square","line":1,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}}}},"statementMap":{"1":{"start":{"line":1,"col
umn":0},"end":{"line":1,"column":37}},"2":{"start":{"line":1,"column":22},"end":{"line":1,"column":35}}},"branchMap":{}};
}
__cov_LQgId1OfxIM2Vvj2j1AaeQ = __cov_LQgId1OfxIM2Vvj2j1AaeQ['.../istanbul/test/instrumentation/test-es
6-arrow-fn.js'];
function square(x){__cov_LQgId1OfxIM2Vvj2j1AaeQ.f['1']++;__cov_LQgId1OfxIM2Vvj2j1AaeQ.s['2']++;return x*x;}

return output;
}

It was named square, but after coverage it becomes anonymous

Most helpful comment

I've recently encountered this issue and I tried to fix it on the istanbul side. I forked babel-plugin-istanbul to fix this, and I put my discovery in here:

https://github.com/istanbuljs/babel-plugin-istanbul/issues/125

Could you folks let me know what you think about my proposal?

All 6 comments

That instrumented code seems to be missing some essential components: output is never set, the instrumented square function is never exported out of the wrapping function, does it even work? Aside from the function name.

I have this same issue when creating snapshots in Jest with enzyme shallow rendering.
say for example I have this:
const MyComponent () => (<div><NestedComponent/></div>);

When I run with coverage the snapshot renders as <div><Component /></div>
When I run without coverage the snapshot renders as <div><NestedComponent /></div>

this because https://github.com/airbnb/enzyme/blob/master/src/Debug.js#L26 default to Component in the specified case

@mikewoudenberg Yeah it's super annoying. Using function statements instead of expressions and arrow functions fixes it, but it shouldn't actually matter how you define your component. Unfortunately this particular issue has existed with istanbul for quite some time now..

Workaround idea. You could enforce using named function using the func-style eslint plugin.

func-style: ["error", "declaration"] should enforce using function declarations.

Any update on this issue?

I've recently encountered this issue and I tried to fix it on the istanbul side. I forked babel-plugin-istanbul to fix this, and I put my discovery in here:

https://github.com/istanbuljs/babel-plugin-istanbul/issues/125

Could you folks let me know what you think about my proposal?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lizhexia picture lizhexia  路  60Comments

gbahamondezc picture gbahamondezc  路  24Comments

dcrockwell picture dcrockwell  路  37Comments

blindenvy picture blindenvy  路  19Comments

Raynos picture Raynos  路  27Comments