Error SyntaxError: Unexpected token with jest and react-native

I have a problem to configure jest with react-native, when I install a third library I have SyntaxError with jest.
I installed react-native-radio-buttons-group and I have this error:

    Details:

    D:\Documents\aviturn\design-system\node_modules\react-native-radio-buttons-group\lib\index.ts:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from './types';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

It’s also important to note that I had a similar problem with the library react-native-bouncy-checkbox.

Here is my jest config:

module.exports = {
  preset: 'react-native',
  setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
  moduleDirectories: ['node_modules', 'src'],
};

And here my babe config:

module.exports = {
  presets: ["module:@react-native/babel-preset"],
  plugins: [
    "transform-inline-environment-variables",
    ["babel-plugin-react-docgen-typescript", { exclude: "node_modules" }],
    [
       'module-resolver',
       {
         root: ['./src'],
         extensions: ['.ios.js', '.android.js', '.js', '.ts', '.tsx', '.json'],
         alias: {
           "@components": "./src/components",
           "@styles": "./src/styles",
           "@core": "./src/core"
         }
       }
    ]
  ]
};

Thank you in advance, I searched for a long time but impossible to fix the issue.

I ended up finding the solution I had to add the library to the transformIgnorePatterns. By default all modules in node_modules are not transformed, so the solution is to tell jest to transform it.

  transformIgnorePatterns: [
'node_modules/(?!((jest-)?react-native|@react-native(-community)?)|react-native-radio-buttons-group/)',]

So now every modules in node_modules are not transformed except react-native, @react-native, @react-native-community and my library

Leave a Comment