【React】Jestが動かないときの対処法(MacOS Sierra)

【React】Jestが動かないときの対処法(MacOS Sierra)

どうも、イソップです。

Reactではテストライブラリとして、Jestを使っているのですが、
ある時起動できずにものすごく困ったので、対処法を紹介します。

エラーが出てそもそも実行できない

$ yarn test                                                                                                                                                                         
yarn test v0.17.8
$ node scripts/test.js --env=jsdom 
✨  Done in 0.58s.
Determining test suites to run...2017-04-27 15:15 node[5590] (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-22)
2017-04-27 15:15 node[5590] (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-22)
2017-04-27 15:15 node[5590] (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-22)
2017-04-27 15:15 node[5590] (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-22)
events.js:163
      throw er; // Unhandled 'error' event
      ^

Error: Error watching file for changes: EMFILE
    at exports._errnoException (util.js:1034:11)
    at FSEvent.FSWatcher._handle.onchange (fs.js:1376:11)
error Command failed with exit code 1.

なんでやねん。

解決方法

$ brew install watchman

homebrew を使って watchman という男をインストールします。

$ yarn test   

No tests found related to files changed since last commit.
Press `a` to run all tests, or run Jest with `--watchAll`.

Watch Usage
 › Press a to run all tests.
 › Press o to only run tests related to changed files.
 › Press p to filter by a filename regex pattern.
 › Press q to quit watch mode.
 › Press Enter to trigger a test run.

無事動きました。

watchman はFacebookが開発しているファイル監視ツールで、
今回のJestやreact-nativeなんかが依存している模様です。

MacOS Sierra の環境で動かない場合は一度試してみてください。

五十川 洋平(Yohei Isokawa)

五十川 洋平(Yohei Isokawa)

フロントエンドエンジニア/面白法人カヤックなどのWeb制作会社に勤務したのち、故郷の新潟に戻り独立。JSフレームワークAngularやFirebase、Google Cloud Platformを使ったWebアプリ開発が得意。 また、Udemyのプログラミング解説の講師、writer.appの自主開発や上越TechMeetupの主催などを行っています。

プロフィール

©Copyright 2022 Yohei Isokawa All Rights Reserved.