React Nativeにaws-amplifyをインポートしたらSyntaxErrorでハマった
create-react-native-appでアプリを作成し、aws-amplifyをインポートし、テストすると、下記のようなエラーが発生します。
import Amplify from 'aws-amplify'; import aws_exports from './aws-exports';
● Test suite failed to run /???/???/node_modules/aws-amplify-react-native/dist/index.js:14 import { default as AmplifyCore, I18n } from 'aws-amplify'; ^^^^^^ SyntaxError: Unexpected token import > 1 | import React from 'react'; 2 | import { StyleSheet, Text, View } from 'react-native'; 3 | import Amplify, { withAuthenticator } from 'aws-amplify-react-native'; at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:316:17) at Object.<anonymous> (App.js:1:884)||<
原因はnode_modules/aws-amplify-react-native/dist/index.js:14がトランスパイルされていないことです。
なので、トランスパイルするようにpackage.jsonに次のような記述をするとテストが通ります。
"jest": { "preset": "jest-expo", "transformIgnorePatterns": [] }
ただ、この設定だと余計なファイルも含めてすべてトランスパイルされるのでテストに時間がかかります。
なのでyarn testして1回トランスパイルしたあと、transformIgnorePatternsを削除するとテストが速く動きます。
"jest": { "preset": "jest-expo", }