istanbul icon indicating copy to clipboard operation
istanbul copied to clipboard

istanbul instrumentation function.toString()

Open paulhoconnor opened this issue 11 years ago • 6 comments

any downstream interpretation of function.toString(), as instrumented by istanbul, sees instrumentation code, interprets as garbage, and throws a ReferenceError. An example of this is eval'ing functions into different threads.

/* istanbul ignore next */ should cause the next 'thing' not to be instrumented

paulhoconnor avatar Feb 08 '15 12:02 paulhoconnor

ignore is just reporting fakery now. Doesn't mean "do not instrument". Need to look into this

gotwarlost avatar Feb 19 '15 03:02 gotwarlost

Have the same problem.

I send function to MongoDB like:

function filter(){
 ...
}

User.find({
    $where: filter.toString()
}, function( error, users ){
    // MongoError: ReferenceError: __cov_G1RH9TezwmrgBKIjkjZ4FQ is not defined
});

Do you know any way to avoid this?

ioncreature avatar Aug 05 '15 12:08 ioncreature

+1 met the same issue. the fn is passed into another process and becomes

'function (){__cov_gjt0lukdoxUzQSDPHovUcQ.f[\'66\']++;__cov_gjt0lukdoxUzQSDPHovUcQ.s[\'219\']++;return document.title;}'

but there is no __con_* in that different context

Unhandled rejection ReferenceError: __cov_gjt0lukdoxUzQSDPHovUcQ is not defined

fritx avatar Feb 28 '16 15:02 fritx

My solution:

fnstr = fnstr.replace(/__cov_(.+?)\+\+;?/g, '')

fritx avatar Feb 28 '16 15:02 fritx

Brilliant!! I was having the same problem, and this is a beautiful solution.

I made one addition...

fnstr = fnstr.replace(/__cov_(.+?)\+\+[,;]?/g, '')

I changed the ? to [,?].

This is because my .toString() result included an if statement with multiple items if (address > -1 && program >= 1 && program <= 4) {

Istanbul generated:

((__cov_x3uoF3uO18CbkEPjMSjaKw.b['9'][0]++,address>-1)&&(__cov_x3uoF3uO18CbkEPjMSjaKw.b['9'][1]++,program>=1)&&(__cov_x3uoF3uO18CbkEPjMSjaKw.b['9'][2]++,program<=4)){__cov_x3uoF3uO18CbkEPjMSjaKw.b['8'][0]++;__cov_x3uoF3uO18CbkEPjMSjaKw.s['24']++;if((__cov_x3uoF3uO18CbkEPjMSjaKw.b['11'][0]++,speed<=450)||(__cov_x3uoF3uO18CbkEPjMSjaKw.b['11'][1]++,speed>=3450)||(__cov_x3uoF3uO18CbkEPjMSjaKw.b['11'][2]++,isNaN(speed))||(__cov_x3uoF3uO18CbkEPjMSjaKw.b['11'][3]++,speed==null))

Notice the __cov_x3uoF3uO18CbkEPjMSjaKw.b['9'][0]++, ending , instead of ;.

Original regex resulted in if ((, address > -1) && (, program >= 1) && (, program <= 4)) {

With the modification the result is clean, running code.

Thanks @fritx !

tagyoureit avatar Jan 24 '17 18:01 tagyoureit

Another workaround is to have 2 same functions: first with ignore and second one without it.

export function fn () { ... }

// istanbul ignore next
function fnCopy () { ... }

... fnCopy.toString() ...

fn exported and covered, fnCopy used inside another context without istanbul.

But this workaround is ugly and I don't like it.

andrew-aladev avatar Jun 29 '20 11:06 andrew-aladev