webpack-contrib / istanbul-instrumenter-loader

Istanbul Instrumenter Loader
MIT License
273 stars 65 forks source link

Wrong coverage output with Babel #86

Open aaron61591 opened 5 years ago

aaron61591 commented 5 years ago

this is my webpack configuration in karma.conf.js:

webpack: {
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [{ loader: 'babel-loader' }],
        exclude: /(node_modules)/,
      },
      {
        test: /\.js$/,
        use: [{
          loader: 'istanbul-instrumenter-loader',
          query: { esModules: true },
        }],
        include: /(src)/,
        enforce: 'post',
      },
    ],
  },
}

html reporter result:

2018-09-28 3 16 17

instrumented file like this:

cov_20zyf0jfnw.s[0]++;
Object.defineProperty(exports, "__esModule", {
  value: true
});
cov_20zyf0jfnw.s[1]++;
exports.default = exports.network = exports.fit = void 0;
var _fit = (cov_20zyf0jfnw.s[2]++, _interopRequireDefault(require("./fit")));
var _network = (cov_20zyf0jfnw.s[3]++, _interopRequireDefault(require("./network")));

function _interopRequireDefault(obj) {
  cov_20zyf0jfnw.f[0]++;
  cov_20zyf0jfnw.s[4]++;
  return (cov_20zyf0jfnw.b[1][0]++, obj) && (cov_20zyf0jfnw.b[1][1]++, obj.__esModule) ? (cov_20zyf0jfnw.b[0][0]++, obj) : (cov_20zyf0jfnw.b[0][1]++, {
    default: obj
  });
}
var fit = (cov_20zyf0jfnw.s[6]++, _fit.default);
cov_20zyf0jfnw.s[7]++;
exports.fit = fit;
var network = (cov_20zyf0jfnw.s[8]++, _network.default);
cov_20zyf0jfnw.s[9]++;
exports.network = network;
var _default = (cov_20zyf0jfnw.s[10]++, {
  fit: fit,
  network: network
});
cov_20zyf0jfnw.s[11]++;
exports.default = _default;

if I update my webpack configuration, and run the instrumentation as a per step.

everything is ok.

mcsmcs commented 5 years ago

Can confirm switching to enforce: 'pre' corrects the output.

foolip commented 5 years ago

I'm seeing a similar problem with the number of lines being affected by the combination of babel-loader and istanbul-instrumenter-loader. https://github.com/webpack-contrib/istanbul-instrumenter-loader#with-babel suggests enforce: 'post' which is surprising. I'll try both that and enforce: 'pre' as seems to have worked for @mcsmcs.

foolip commented 5 years ago

I've tried both post and pre now: https://github.com/GoogleChromeLabs/confluence/pull/403 https://github.com/GoogleChromeLabs/confluence/pull/404

As expected, enforce: 'pre' is what fixes the line numbering, since the instrumentation is otherwise done on the babel output. I've sent https://github.com/webpack-contrib/istanbul-instrumenter-loader/pull/93 in the hope that it's just a typo in the documentation, but I'm not certain that it is, something else might break?