LoginSignup
2
0

More than 1 year has passed since last update.

Jestを使おうとしたら「SyntaxError: Unexpected token '.'」エラーが発生した件について

Last updated at Posted at 2023-01-08

1.エラー内容

npm test実行時にエラーが発生しました。

$ ./node_modules/.bin/jest --init
/var/www/workspace/node_modules/jest/node_modules/jest-cli/build/run.js:129
    if (error?.stack) {
              ^

SyntaxError: Unexpected token '.'

2.なぜ発生したのか

結論から言うと、JestのバージョンとJavascriptのバージョンの互換性がなかったためです。
Jestインストール時に自動生成したコードで使用されていnull 安全演算子は、JavaScript(ES11)のバージョンからサポートされていました。私の環境では古いバージョンのJavascriptを使用していたため発生していました。
(ただ、npm updateをしてもなお発生していたのでハマっていました、、)

3.解決方法

2020年にリリースされたjestを使用することでエラーなく動きました。

4.手順

#一度jestをアンインストール
$ rm -rf ./node_modules/jest
$ npm uninstall jest

#古いバージョンのjestを再インストール
$ npm install --save-dev jest@24.9.0
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0