posted: 2018/04/21

babel@7の環境でjest使う時に頑張る必要がある話

babelの7系を利用しつつjestを利用すると、こんなエラーが出てしまう。
    Requires Babel "^7.0.0-0", but was loaded with "6.26.0". If you are sure you have a compatible version of @babel/core, it is likely that something in your build process is loading the wrong version. Inspect the stack trace of this error to look for the first entry that doesn't mention "@babel/core" or "babel-core" to see what is calling Babel.
jestとbabel@7の相性があまり良くないようで、@babel/core, babel-coreをどっちも入れたりbabel-jestを使わないといけない
あんまり裏側は追ってないが、とりあえずこんな感じ
$ npm install babel-core@^7.0.0-0 @babel/core babel-jest regenerator-runtime
一応これで動くはずだが、動かない場合はbabel-jestを挟むと動く場合がありそう。
    "transform": {
      "^.+\\.jsx?$": "babel-jest"
    },
Edit on Github