如何使用 karma-babel-preprocessor 和 PhantomJs 保留行正确的行号?

How to preserve line correct line numbers with karma-babel-preprocessor and PhantomJs?

当我像这样使用 karma-babel-preprocessor 的 documented configuration

module.exports = function (config) {
  config.set({
    preprocessors: {
      'src/**/*.js': ['babel'],
      'test/**/*.js': ['babel']
    },
    babelPreprocessor: {
      options: {
        sourceMap: 'inline'
      },
      filename: function (file) {
        return file.originalPath.replace(/\.js$/, '.es5.js');
      },
      sourceFileName: function (file) {
        return file.originalPath;
      }
    }
  });
};

我得到错误的源代码行号,例如

 at /var/www/edu-web/tests/jasmine/services/image/imageServiceTest.es5.js:77

默认情况下不支持源映射,因此不足为奇。但是,这里的问题是 imageServiceTest.es5.js 文件在 karma 完成后被删除,所以我别无选择,只能猜测单元测试失败的地方(在哪一行),这很慢。

issue 中有 Chrome 的解决方案(不是 PhantomJs)。我也可以修复 PhantomJs 的配置吗?

我在查看 Babel 文档时找到了解决方案:

retainLines Retain line numbers. This will lead to wacky code but is handy for scenarios where you can't use source maps.

NOTE: This will obviously not retain the columns.

(http://babeljs.io/docs/usage/options/)

我的最终配置:

module.exports = function (config) {
  config.set({
    preprocessors: {
      'src/**/*.js': ['babel'],
      'test/**/*.js': ['babel']
    },
    babelPreprocessor: {
      options: {
        sourceMap: 'inline',
        retainLines: true // NEW LINE
      },
      filename: function (file) {
        return file.originalPath.replace(/\.js$/, '.es5.js');
      },
      sourceFileName: function (file) {
        return file.originalPath;
      }
    }
  });
};