Mocha ES6 Babel "TypeError: undefined is not a function"

Mocha ES6 Babel "TypeError: undefined is not a function"

我目前正在尝试为我的 JavaScript 组件实施简单测试,但我收到错误:TypeError: undefined is not a function 来自我的测试。

这是我的 class 中这一行的错误:

this.options = Object.assign({}, this.defaults, options);

this.defaults是一个默认选项的对象,options目前是一个没有任何内容的对象。

我是 运行 来自 CLI 的代码,使用 npm test 解析为:mocha ./src/components/myComponent/myComponent.spec.js --compilers js:babel-register

有谁知道为什么会出现这个错误?

你是什么版本的节点运行? Object.assign 不会始终可用。您可能想使用对象分配包

const assign = require('object-assign');
...
this.options = assign({}, this.defauts, options)

Babel 需要插件才能转换Object.assign,请尝试安装babel-plugin-transform-object-assign