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

3
当使用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"
  ]
}

注意:这种情况发生在我的应用程序入口点是否需要'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)
1个回答

5

Sourcemaps 和 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')));

你还需要拥有

require('source-map-support').install();

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接