jestのチュートリアルで躓いた話


はじめに

jestを覚えたいと思った。
チュートリアルを実行した。
いきなりエラーが発生した。

npmのバージョン

❯ npm -v
5.6.0

構成

mkdir jest-spike
❯ cd jest-spike
❯ npm init
❯ npm install --save-dev jest
❯ vim sum.js (サンプルの通り)
❯ vim sum.test.js (サンプルの通り)  
❯ vim package.json (サンプルの通り) 

エラーの内容

❯ npm test

> [email protected] test /Users/xx/workspace/jest-spike
> jest

/Users/xx/workspace/jest-spike/node_modules/jest/node_modules/jest-cli/build/cli/index.js:227
    } catch {
            ^

SyntaxError: Unexpected token {
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/xx/workspace/jest-spike/node_modules/jest/node_modules/jest-cli/bin/jest.js:16:3)
npm ERR! Test failed.  See above for more details.

原因

nodeのバージョンが古すぎた。
yarnでjest入れたらこんなエラー出た。

error [email protected]: The engine "node" is incompatible with this module. Expected version ">= 10.14.2". Got "8.11.1"
error Found incompatible module.

nodeをv8.11.1からv14.8.0に上げたら動いた。