3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Jest encountered an unexpected token解決法

Posted at

react-native-cameraを使ったアプリのテストをJestでする際、こんなエラーに遭遇した

  TypeError: Cannot read property 'Aspect' of undefined

  at Object.<anonymous> (node_modules/react-native-camera/src/Camera.js:425:113)
  at Object.<anonymous> (node_modules/react-native-camera/src/index.js:3:38)

他にもreact-native-unimodulesで

Jest encountered an unexpected token

なんかが出たりする

結論から先に書くと、これらのエラーは

jest.mock('{モジュール名}', () => '{クラス名}');

でモック化すると解決した。

react-native-cameraなら

jest.mock('react-native-camera', () => 'Camera');

となる。

Jest公式によると、react-nativeに組み込まれたJestプリセットにはデフォルトのモックが付属しているものの、いくつかのコンポーネントにはそれが無く、ネイティブコードに依存しているためマニュアルでモック化する必要があるとかなんとか

正直Jestのモックとはそもそも何ぞや?というところからわかっておらず現状おまじないと化してるので、後で調べておきたい

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?