[英]Running mocha with babel es2015 not working properly
I'm trying really hard to get Babel 6 to work for me. 我非常努力地让Babel 6为我工作。 I use 5 quite successfully for my day job (for React development), but 6 doesn't seem to be integrating with Mocha as expected. 我的日常工作非常成功地使用了5(用于React开发),但是6似乎并没有像预期的那样与Mocha集成。
I have these devDependencies
, scripts, and babel configuration: 我有以下devDependencies
,脚本和babel配置:
{
"devDependencies": {
"babel-cli": "^6.1.2",
"babel-core": "^6.1.2",
"babel-preset-es2015": "^6.1.2",
"mocha": "^2.3.3"
},
"scripts": {
"test": "mocha --compilers js:babel-core/register ./tests --recursive"
},
"babel": {
"presets": [
"es2015"
]
}
}
Here's my test code: 这是我的测试代码:
import ObjectBeingTested from '../src/object-being-tested';
describe('ObjectBeingTested', () => {
it('does stuff', () => {
const obj = new ObjectBeingTested({ foo: 0, bar: 1 });
// ...
});
});
...and the source code has: ...并且源代码有:
export default class ObjectBeingTested {
constructor({ foo, bar}) {
this.foo = foo;
this.bar = bar;
}
}
However, when running, I get foo is not defined
in the first line of the constructor. 但是,在运行时,我foo is not defined
在构造函数的第一行中foo is not defined
。 Interestingly, if I transpile the code directly and call it directly through the node CLI, it works fine. 有趣的是,如果我直接转换代码并直接通过节点CLI调用它,则可以正常工作。 Here's what babel-cli
produced for the file: 这是babel-cli
为该文件生成的内容:
"use strict";
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
Object.defineProperty(exports, "__esModule", {
value: true
});
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var ObjectBeingTested = (function () {
function ObjectBeingTested(_ref) {
_classCallCheck(this, ObjectBeingTested);
var foo = _ref.foo;
var bar = _ref.bar;
this.foo = foo;
this.bar = bar;
}
_createClass(ObjectBeingTested, [/*...other defs */]);
return ObjectBeingTested;
})();
exports.default = ObjectBeingTested;
How do I properly run mocha to transpile tests & anything they import? 如何正确运行Mocha来传递测试及其导入的内容?
Things I've tried: 我尝试过的事情:
.babelrc
file instead; 将babel配置移到.babelrc
文件中; there's no difference. 没有区别。 -r babel-core/register
instead of --compilers
also does not work. 使用-r babel-core/register
代替--compilers
也不起作用。 Update 更新资料
This is interesting. 这是有趣的。 I decided to do a console.log(ObjectBeingTested.toString())
after the import to see what mocha was getting; 我决定在导入后执行console.log(ObjectBeingTested.toString())
,以查看摩卡咖啡得到了什么; here's what it outputs: 这是它的输出:
function ObjectBeingTested(_ref) {
_classCallCheck(this, ObjectBeingTested);
this.foo = foo;
this.bar = bar;
}
Notice the two dereferencing lines are completely missing. 请注意,两个取消引用行完全丢失。
Update 2 : 更新2 :
The issue has nothing to do with mocha; 这个问题与摩卡咖啡无关。 I can reproduce that imported modules are not transpiled the same way as those transpiled in bulk. 我可以重现,导入的模块的移植方式与批量移植的方式不同。
I found a workaround. 我找到了解决方法。 First off, I switched to Node 5 (from 0.12) so that I would need less transformation plugins for working code. 首先,我从0.12切换到Node 5(节点5),这样我就需要更少的转换插件来工作代码。 I first tried the es2015-node5
preset, but that still didn't work. 我首先尝试了es2015-node5
预设,但仍然无法正常工作。 So instead, I pulled in these specific plugins in my .babelrc
: 因此,我在.babelrc
插入了这些特定的插件:
{
"plugins": [
"transform-es2015-modules-commonjs",
"transform-es2015-parameters",
"transform-es2015-destructuring"
]
}
With these, my constructor finally transpiled correctly. 有了这些,我的构造函数终于正确地进行了编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.