From 68872838e4aefd4c0d851cd78451842defb4ffe3 Mon Sep 17 00:00:00 2001 From: David Manthey Date: Fri, 24 Aug 2018 11:45:30 -0400 Subject: [PATCH] Fix reporting coverage when using babel. If the istanbul-instrumenter-loader is added in a separate rule from the babel-loader, the line numbers reported by coverage are wrong. --- karma-base.js | 2 +- karma-cov.conf.js | 12 +++++------- webpack.base.config.js | 28 ++++++++++++++++++++++++---- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/karma-base.js b/karma-base.js index bd83e07e64..366771fc1c 100644 --- a/karma-base.js +++ b/karma-base.js @@ -1,4 +1,4 @@ -var webpack_config = require('./webpack.config'); +var webpack_config = require('./webpack.base.config'); var url = require('url'); var fs = require('fs'); var path = require('path'); diff --git a/karma-cov.conf.js b/karma-cov.conf.js index 4067c16e22..272cc840c6 100644 --- a/karma-cov.conf.js +++ b/karma-cov.conf.js @@ -1,8 +1,6 @@ // Defines a test server for running jasmine unit tests // with coverage support. -var path = require('path'); - /** * Return URL friendly browser string */ @@ -40,11 +38,11 @@ module.exports = function (config) { {type: 'text'} ] }; - karma_config.webpack.module.rules.unshift({ - test: /\.js$/, - include: path.resolve('src/'), - exclude: path.resolve('src/polyfills.js'), - use: ['istanbul-instrumenter-loader'] + /* Alter our first webpack module rule which should just apply to src/*.js + * files. */ + karma_config.webpack.module.rules[0].use.push({ + loader: 'istanbul-instrumenter-loader', + options: {esModules: true} }); config.set(karma_config); diff --git a/webpack.base.config.js b/webpack.base.config.js index 8afbbb0d69..094059bb46 100644 --- a/webpack.base.config.js +++ b/webpack.base.config.js @@ -75,12 +75,32 @@ module.exports = { */ module: { rules: [{ + /* The first rule only includes src/*.js so that it can conveniently be + * modified for istanbul instrumentation */ + test: /\.js$/, + include: path.resolve('src'), + exclude: path.resolve('src/polyfills.js'), + use: [{ + loader: 'babel-loader', + options: { + presets: [[ + 'env', { + targets: { + browsers: ['defaults'], + uglify: true + }, + useBuiltIns: 'usage' + } + ]], + cacheDirectory: true + } + }] + }, { test: /\.js$/, include: [ - path.resolve(__dirname, 'src'), - path.resolve(__dirname, 'tests'), - path.resolve(__dirname, 'examples'), - path.resolve(__dirname, 'tutorials') + path.resolve('tests'), + path.resolve('examples'), + path.resolve('tutorials') ], use: [{ loader: 'babel-loader',