gotwarlost / istanbul

Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale.
Other
8.7k stars 785 forks source link

deep-destructure instrumentation bug #861

Open osher opened 6 years ago

osher commented 6 years ago

Scenario

Consider the following snippet:

module.exports = function serverFactory(ioc) {
    const {
      events: { EventEmitter } = require('events')
    , destroyable              = require('server-destroy')
    , serverPromise            = require('./web/server')
    , logger
    , args
    } = ioc
   /*
     ...
   */
}

The module exports a factory that expects an inversion-of-control context, and extracts from it what it needs using a deep destructure. The code runs well and passes tests.

expected

Coverage should pass the same like the tests do

found

Using the same tests for coverage fails because instrumented it looks (when beautified a little) like so:

module.exports = function serverFactory(ctx){
  __cov_gAjmS2LR4S03hyyjcVwIug.f['1']++;
  __cov_gAjmS2LR4S03hyyjcVwIug.s['2']++;
  const {
    {EventEmitter}=require('events'):{EventEmitter}=require('events'),
    destroyable=require('server-destroy'),
    serverPromise=require('./web/server'),
    logger,
    args
  } = ctx;
  __cov_gAjmS2LR4S03hyyjcVwIug.s['3']++;
  /**
   ...
  */
}

raw:

__cov_gAjmS2LR4S03hyyjcVwIug.s['1']++;module.exports=function serverFactory(ctx){__cov_gAjmS2LR4S03hyyjcVwIug.f['1']++;__cov_gAjmS2LR4S03hyyjcVwIug.s['2']++;const {{EventEmitter}=require('events'):{EventEmitter}=require('events'),destroyable=require('server-destroy'),serverPromise=require('./web/server'),logger,args}=ctx;__cov_gAjmS2LR4S03hyyjcVwIug.s['3']++;};