Babel + Mocha 堆栈跟踪报告错误的行号

aus*_*eow 3 mocha.js node.js babeljs

使用 Babel 6 和 Mocha 时,堆栈跟踪报告错误的行号。我很确定这是因为转译添加了额外的代码。对我来说,这是 Babel 6 与 Babel 5.x 中的新行为。有人有关于如何在使用 Mocha 进行单元测试时解决此问题的解决方案吗?

这是我的 .babelrc 配置:

{
  "ignore": [
    "node_modules",
    "bower_components"
  ],
  "presets": [
    "es2015",
    "react"
  ],
  "plugins": [
    "transform-react-constant-elements",
    "syntax-async-functions",
    "transform-regenerator"
  ]
}
Run Code Online (Sandbox Code Playgroud)

注意:无论我是否在应用程序的入口点 require('babel-polyfill') ,都会发生这种情况。

示例堆栈跟踪如下所示:

TypeError: Cannot read property 'should' of undefined
    at _callee2$ (test/unit/index.test.js:217:34)
    at step (test/unit/index.test.js:27:284)
Run Code Online (Sandbox Code Playgroud)

lag*_*lex 5

源图和retainLines:true选项。下面是一个 Gulp 任务示例:

const babel = require('gulp-babel');
const sourcemaps = require('gulp-sourcemaps');

gulp.task('babel', done =>
    gulp.src('src/**/*.es6')
    .pipe(sourcemaps.init())
    .pipe(babel({
        presets: ['es2015', 'stage-0'],
        retainLines: 'true',
    }))
    .pipe(sourcemaps.write('.', {
        sourceRoot: 'src'
    }))
    .pipe(gulp.dest('lib')));
Run Code Online (Sandbox Code Playgroud)

你还必须有

require('source-map-support').install();
Run Code Online (Sandbox Code Playgroud)

在编译代码的顶部(只是入口点,即 package.json 中指定的“主”文件)