简体   繁体   中英

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

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/@codler/react-native-keyboard-aware-scroll-view/lib/index.js",
      "/node_modules/@react-native-community/async-storage/(?!(lib))",
      "/node_modules/native-base-shoutem-theme/.*",
      "node_modules/native-base/.*",
      "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/.*|native-base-*|native-base-shoutem-*)"
    ],
    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:

The error is caused by the below import module inside LoginScreen.js

import {
    Content,
    Container,
    H2,
    Form,
    Item,
    Input,
    Button,
    Text,
    View,
} from "native-base";

Test case inside 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)

在此处输入图片说明

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. Configuration of the project. 项目的当前配置

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

This is an open issue on the Native base end. Further details can be found on this ticket :

https://github.com/GeekyAnts/NativeBase/issues/3105

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM