Help us understand the problem. What is going on with this article?

babel7系 mocha5系のテストコマンド

More than 1 year has passed since last update.

babel 7.0.0-beta.40 mocha5.0.1の組み合わせで使ったらエラーが出た

環境

package.json
{
  "scripts": {
    "test": "mocha --compilers js:babel-register"
...
  },
  "dependencies": {
    "@babel/core": "^7.0.0-beta.40",
    "@babel/runtime": "^7.0.0-beta.40"
  },
  "devDependencies": {
    "@babel/node": "^7.0.0-beta.40",
    "@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.40",
    "@babel/plugin-transform-async-to-generator": "^7.0.0-beta.40",
    "@babel/plugin-transform-runtime": "^7.0.0-beta.40",
    "@babel/preset-env": "^7.0.0-beta.40",
    "@babel/register": "^7.0.0-beta.40",
    "babel-register": "^6.26.0",
    "babel-loader": "^8.0.0-beta.0",
    "chai": "^4.1.2",
    "mocha": "^5.0.1",
    "webpack": "^3.11.0",
    "webpack-node-externals": "^1.6.0"
  }
...
}
.babelrc
{
  "plugins": [
    ["@babel/plugin-transform-runtime", {
      "helpers": false,
      "polyfill": true,
      "regenerator": true,
      "moduleName": "@babel/runtime"
    }],
    ["@babel/plugin-proposal-object-rest-spread"],
    ["@babel/plugin-transform-async-to-generator"]
  ],
  "presets": ["@babel/preset-env"]
}

npm test

> myproject@1.0.0 test path/to/dir
> mocha --compilers js:babel-register

/path/to/dir/myproject/node_modules/@babel/plugin-transform-runtime/lib/index.js:17
  var helpers = options.helpers,
                        ^

TypeError: Cannot read property 'helpers' of undefined

んーん動かない でも書いてあるんだよなあ

スクリーンショット 2018-03-03 7.28.49.png

Upgrade to Babel 7 (WIP)

mochaの方を見ることに。。

--compilers is redundant; we've yet to encounter a real-world situation in which the solution couldn't be expressed using --require.
........
Babel: --compilers js:babel-core/register becomes --require babel-core/register
........
$ mocha --compilers coffee:coffee-script/register --recursive ./test
Then this is how you'd accomplish the same thing (** roughly means "recursive") in v4:

$ mocha --require coffee-script/register "test/*/.js"

^ 適当に抜粋してるので悪しからず。。
mocha - compilers deprecation

--compilersは冗長だからやめて--requireregister君を指定してあげろとそれでbabelの方は--require babel-core/registerだぞと、

babelのベージと擦り合わせてmocha --require babel-registerにしたが、変わらずエラー

まあ根本的な解決になるとは思ってなかったから想定内。

普通に他のbabelのライブラリ同様 @babel/registerを指定してみたらできました。。

結論

$ node_modules/.bin/mocha --require @babel/register

ほとんどbabel7のものを使っていたけど、mochaのbabel-registerの部分も変えなきゃいけなかった、、
まあそりゃバージョン合わせようねってことだね

uron
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away