SyntaxError: Cannot use import statement outside a module: when running Jest-expo tests

Please provide the following:

  1. SDK Version: 38.0.10
  2. Platforms(Android/iOS/web/all): Android/iOS

The configuration for the project. Currently using jest-expo for testing in this project. The version of jest-expo in the project is 39.0.0. Version of Jest installed globally is 26.6.3

package.json :

"scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject",
    "test": "jest",
    "type": "module"
  },
...
"devDependencies": {
    "@babel/core": "^7.12.3",
    "babel-preset-expo": "^8.3.0",
    "jest-expo": "^39.0.0",
    "react-test-renderer": "^17.0.1"
  },
"jest": {
    "preset": "jest-expo"
  }

jest.config.js:

module.exports = {
    setupFilesAfterEnv: [
      './setup-tests.js',
    ],
    "transformIgnorePatterns": [
        "node_modules/(?!(jest-)?react-native|react-clone-referenced-element|@react-native-community|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|@sentry/.*)",
        "/node_modules/@codler/react-native-keyboard-aware-scroll-view/lib/index.js",
        "/node_modules/@react-native-community/async-storage/(?!(lib))",
        "/node_modules/native-base-shoutem-theme/index.js",
        "/node_modules/native-base-shoutem-theme/.*"
    ],
    verbose : true
  };

babel.config.js

module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: ['inline-dotenv'],
  };
};

I am trying to render a snapshot for a login page that is react-native:

LoginScreen.test.js

import LoginScreen from './LoginScreen';
import React from 'react';
import renderer from 'react-test-renderer';

it('renders LoginScreen correctly', () => {
    const tree = renderer.create(<LoginScreen />).toJSON();
    expect(tree).toMatchSnapshot();
});

Test case is throwing the error

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import connectStyle, { clearThemeCache } from "./src/connectStyle";
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1258:14)
      at Object.<anonymous> (node_modules/native-base/src/index.js:1:1)

enter image description here

I have tried other similar answers available on Stack Overflow. But none of them applied to the current project on which I am working with.

I have added connectStyle to the Jest transformIgnorePatterns but still it is throwing that error.

Current Project :
React native mobile application developed using expo.
Using jest-expo for testing.

Tried uninstalling and re-installing all the npm and expo modules, that didn’t help either.

2 Likes

I have raised the same on Stack Overflow and I was told that this is an expo specific issue and to check with expo team.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.