Babel + Mocha堆栈跟踪报告错误的行号 [英] Babel + Mocha stack traces report wrong line number
问题描述
使用Babel 6和Mocha时,堆栈跟踪报告错误的行号.我很确定这是因为转译会添加额外的代码.对我来说,这是Babel 6 vs. Babel 5.x的新行为.在使用Mocha进行单元测试时,是否有人有解决此问题的解决方案?
When using Babel 6 and Mocha, the stack trace reports a wrong line number. I am pretty sure this is because the transpiling adds extra code. This is new behavior in Babel 6 vs. Babel 5.x for me. Does anyone have a solution on how to fix this when using Mocha for unit tests?
这是我的.babelrc配置:
Here is my .babelrc config:
{
"ignore": [
"node_modules",
"bower_components"
],
"presets": [
"es2015",
"react"
],
"plugins": [
"transform-react-constant-elements",
"syntax-async-functions",
"transform-regenerator"
]
}
注意:无论我在应用程序的入口点是否要求("babel-polyfill"),这种情况都会发生.
Note: this is happening whether or not I require('babel-polyfill') at the entry point of my app.
一个示例堆栈跟踪如下:
A sample stack trace looks like this:
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)
推荐答案
Sourcemaps和retainLines:true
选项.这是一个Gulp任务示例:
Sourcemaps and retainLines:true
option. Here's an example Gulp task:
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中指定的主"文件)
at the top of your compiled code (just the entry point, i.e. the "main" file as specified in your package.json)
这篇关于Babel + Mocha堆栈跟踪报告错误的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!