plasticine / inject-loader

💉📦 A Webpack loader for injecting code into modules via their dependencies.
https://www.npmjs.com/package/inject-loader
MIT License
484 stars 47 forks source link

Support code-coverage instrumentation #52

Open cstickel opened 6 years ago

cstickel commented 6 years ago

I tried inject-loader with Typescript and Istanbul coverage.

For the code:

import getFoo from 'getFoo';

The inject-loader adds a logical or-expression. The result looks like:

var getFoo_1 = __getInjection("getFoo") || __webpack_require__(2);

If the depedency is mocked in all tests and the actual module is never used, the right side of the or-expression is never executed. This results in an uncovered branch in the coverage report.

plasticine commented 6 years ago

@mixer2 Hey there — yep, working around Istanbul (coverage in general) is pretty tricky. It’s something I’d love to support but have been unable to find the time to explore how (and if) it can be done. Sorry for the inconvenience.

eugeneford commented 6 years ago

@mixer2, @plasticine, the same in my project. We are using: Karma + Webpack + Istanbul Instrumenter Loader + coverageInstunbulReporter.

We are facing a pretty similar issue. Lets say that we have:

import something from './something'.

const doSomething = () => {
  const a = 50;
  const b = a + 100;
  something(b);
}

export default doSomething;

Test spec would be:

import somethingInjector from 'inject-loader!./something';

describe('something', () => {
  it('should do something', () => {
    const iSomething  = somethingInjector(
      './something': () => console.log('mocked'),
    ).default;

    iSomething();
  })
});

The issue is that coverage report shows that original doSomething() function is completely uncovered.