Ask Your Question

Revision history [back]

This error occurs because Jest uses a tool called Babel to transpile the React Native code before running the tests. Babel helps to convert newer ES6+ syntax into compatible ES5 syntax. However, sometimes Babel can have issues transpiling certain files, such as node_modules. This is because Babel is unable to transpile code that already exists in ES5 format.

In this specific case, the error is occurring in a file located in the node_modules folder of the React Native library. Since this file is third-party code and not written in ES6+ syntax, it doesn't need to be transpiled by Babel. However, Jest is trying to transpile it due to an incorrect configuration.

To resolve this error, you can add the following code snippet to your Jest configuration file (usually jest.config.js):

transformIgnorePatterns: [
  "node_modules/(?!(jest-)?react-native|react-clone-referenced-element|@react-native-community|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|@unimodules/.*|sentry-expo|native-base)",
],

This code will tell Jest to ignore all files located in the node_modules folder, except for those that relate to React Native, React Navigation, Expo, Sentry Expo, or NativeBase. This way, Jest won't try to transpile the third-party code in error-guard.js, and the SyntaxError should be resolved.