Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Istanbul removes function name in coverage #699

Open
ColCh opened this issue Sep 20, 2016 · 6 comments
Open

Istanbul removes function name in coverage #699

ColCh opened this issue Sep 20, 2016 · 6 comments

Comments

@ColCh
Copy link

ColCh commented Sep 20, 2016

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

@peterjwest
Copy link

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.

@mikewoudenberg
Copy link

mikewoudenberg commented May 24, 2017

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

@ghost
Copy link

ghost commented May 24, 2017

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

@0xR
Copy link

0xR commented May 31, 2017

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

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

@kserjey
Copy link

kserjey commented Aug 16, 2017

Any update on this issue?

@tomchentw
Copy link

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:

istanbuljs/babel-plugin-istanbul#125

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants