SyntaxError: Identifier 'jest' has already been declared


create-react-appをejectしてyarn testを実行すると掲題のエラーが発生しました。
たまたま直せたので、直し方を書いておきます。

 FAIL  scripts/test.js
  ● Test suite failed to run

    SyntaxError: Identifier 'jest' has already been declared

      at Runtime._execModule (node_modules/jest-runtime/build/index.js:867:68)

jest がすでに定義されているよ、というエラーです。

このエラーだけだと何が起きているかよくわからなかったんですが、 https://github.com/facebook/create-react-app/issues/1319 を読むと、scripts/test.js もjestのテスト対象になっているせいで発生しているようでした。

jestの対象ディレクトリからscriptsを除外すれば直ります。

jest.config.js
module.exports = {
[...]
  modulePathIgnorePatterns: ["<rootDir>/scripts/"],
[...]