Sourcemap + istanbul / isparta代码覆盖率为webpack + babel(对于es6)+ mocha(+ karma)

问题描述 投票:3回答:2

我在https://github.com/danielbush/webpack-babel-karma-sourcemap-coverage有一个测试项目。

它由src/中的两个简单源文件组成,其中一个有故意错误(src/lib2.js)。

我想使用从ES6到ES5的babel进行webpack和转换,并使用Karma在此捆绑包上运行Mocha测试。

我已经添加了源映射并对其进行了测试,以便我可以看到原始文件的行号。

对我来说,堆栈跟踪看起来像这样:

21 02 2016 16:03:15.445:INFO [karma]: Karma v0.13.21 server started at http://localhost:9876/
21 02 2016 16:03:15.451:INFO [launcher]: Starting browser Chrome
21 02 2016 16:03:16.551:INFO [Chrome 48.0.2564 (Linux 0.0.0)]: Connected on socket /#yRS32ons0_2HGhrwAAAA with id 3072946

START:
  lib1
    ✖ should return 1

Finished in 0.015 secs / 0.001 secs

SUMMARY:
✔ 0 tests completed
✖ 1 test failed

FAILED TESTS:
  lib1
    ✖ should return 1
      Chrome 48.0.2564 (Linux 0.0.0)
    Error: SIMULATED ERROR
        at foo (/home/danb/projects/so-test-project/tests.webpack.js:135:10 <- webpack:///src/lib2.js:13:8)
        at Context.<anonymous> (/home/danb/projects/so-test-project/tests.webpack.js:93:31 <- webpack:///test/index.js:7:11)

在上面的第二行,它显示了webpack:///src/lib2.js:13,它是原始来源中的正确位置。

为了得到那个映射,我做了这个:https://github.com/danielbush/webpack-babel-karma-sourcemap-coverage/commit/6ea992cae499dccc68488bcb3eaca642ae4ed24e

我想要做的是使用像istanbul(或使用istanbul的isparta)之类的东西来添加覆盖率,这样我就可以在coverage中生成HTML覆盖率报告/显示未涵盖的行(可能) - 以及整体覆盖率百分比。

但我想确保我的堆栈跟踪仍然像上面那样正确映射源。

我已经尝试过isparta-loader和isparta-instrumenter-loader来转换和检测webpack中有问题的文件,但我的行号在stacktrace中略有错误(上图)。似乎有许多类似isparta的东西漂浮在周围,文档不是超级友好的。

更广泛地表达所有这些:我正在捆绑文件和测试,然后在浏览器中测试它(业力或更多手动方式),我希望我的源地图(在浏览器中)指向原始行代码,同时我想要一个覆盖率报告来引用未覆盖的原始行和文件。

javascript webpack istanbul karma-coverage isparta
2个回答
2
投票

我没有针对单个业力/ webpack配置的解决方案。我的解决方法是有2个业力混乱 - 我对atm非常满意。 (我还有其他非业力设置来运行测试。)

一个conf在后台运行测试,为我的es6代码提供准确的堆栈跟踪,另一个使用isparta-loader运行覆盖以转换和检测我的源代码。 (isparta-loader使用isparta,使用伊斯坦布尔)。

所以我的“跑步测试”业力骗局看起来有点像这样:

var webpack = require('webpack');
module.exports = function (config) {
  config.set({
    browsers: [ 'PhantomJS' ],
    singleRun: false,
    frameworks: [ 'mocha' ], // Use the mocha test framework
    files: [
      'tests.webpack.js'
    ],
    preprocessors: {
      'tests.webpack.js': [ 'webpack', 'sourcemap' ]
    },
    reporters: [ 'mocha' ],
    webpack: {
      devtool: 'inline-source-map',
      module: {
        loaders: [
          // Use imports loader to hack webpacking sinon.
          // https://github.com/webpack/webpack/issues/177
          {
            test: /sinon\.js/,
            loader: "imports?define=>false,require=>false"
          },
          {
            test: /\.js$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
            query: { presets: ['es2015'] }
          }
        ]
      }
    },
    webpackServer: {
      noInfo: true
    }
  });
}

我的报道karma conf看起来有点像这样:

var webpack = require('webpack'),
    path = require('path');
module.exports = function (config) {
  config.set({
    browsers: [ 'PhantomJS' ],
    singleRun: true,
    frameworks: [ 'mocha' ],
    files: [
      'tests.webpack.js'
    ],
    preprocessors: {
      'tests.webpack.js': [ 'webpack' ]
    },
    reporters: [ 'coverage' ],
    webpack: {
      // *optional* babel options: isparta will use it as well as babel-loader
      babel: {
        presets: ['es2015']
      },
      // *optional* isparta options: istanbul behind isparta will use it
      isparta: {
        embedSource: true,
        noAutoWrap: true,
        // these babel options will be passed only to isparta and not to babel-loader
        babel: {
          presets: ['es2015']
        }
      },
      module: {
        loaders: [
          {
            test: /sinon\.js/,
            loader: "imports?define=>false,require=>false"
          },
          // Perform babel transpiling on all non-source, test files.
          {
            test: /\.js$/,
            exclude: [
              path.resolve('node_modules/'),
              path.resolve('lib/')
            ],
            loader: 'babel-loader',
            query: { presets: ['es2015'] }
          },
          // Instrument source files with isparta-loader (will perform babel transpiling).
          {
            test: /\.js$/,
            include: [
              path.resolve('lib/')
            ],
            loader: 'isparta'
          }
        ]
      }
    },
    webpackServer: {
      noInfo: true
    },
    coverageReporter: {
      reporters: [
        { type: 'html', dir: 'coverage/' },
        { type: 'text' }
      ]
    }
  });
}

第二个conf提供了一个文本报告,可以在终端中提供即时覆盖摘要,以及html报告,它提供了未经测试的代码的优秀源文件突出显示。 (对于sinon而言,这两者都存在问题,这与此问题无关。)tests.webpack.js使用webpack的require.context来引入我用mocha编写的浏览器测试。并且需要各种karma- *插件。


0
投票

你可能有更好的运气使用伊斯坦布尔的新official Babel Plugin来检测代码覆盖的来源。

根据我的经验,与isparta相比,它提供了更准确的覆盖率报告,除了将其添加到我的Babel配置中的插件列表之外,基本上不需要任何配置。

当我在我的项目中使用它时,源图似乎“开箱即用”。

© www.soinside.com 2019 - 2024. All rights reserved.