javascript - 语法错误 : Cannot use import statement outside a module: when running Jest-expo tests

标签 javascript react-native jestjs babeljs expo

项目的配置。目前在这个项目中使用 jest-expo 进行测试。项目中jest-expo的版本是39.0.0。全局安装的 Jest 版本是 26.6.3
包.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'],
  };
};

我正在尝试为 react 原生的登录页面呈现快照:
错误是由中的以下导入模块引起的LoginScreen.js
import {
    Content,
    Container,
    H2,
    Form,
    Item,
    Input,
    Button,
    Text,
    View,
} from "native-base";
内部测试用例 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();
});

测试用例抛出错误
({"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
我已经尝试过 Stack Overflow 上提供的其他类似答案。但它们都没有适用于我正在处理的当前项目。
我已添加 connectStyle开 Jest transformIgnorePatterns但它仍然抛出该错误。
当前的项目 :
使用 expo 开发的 React native 移动应用程序。
使用 jest-expo 进行测试。
项目的配置。
Current configuration of the project
尝试卸载并重新安装所有 npm 和 expo 模块,但这也无济于事。

最佳答案

这是原生基础端的一个未解决问题。可以在这张票上找到更多细节:
https://github.com/GeekyAnts/NativeBase/issues/3105

关于javascript - 语法错误 : Cannot use import statement outside a module: when running Jest-expo tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64771728/

相关文章:

react-native - 我在哪里可以找到 React Native 支持的所有样式属性列表?

javascript - 在响应函数中调用await

reactjs - 当 CSS 使用 @import 时 Jest 失败

reactjs - 如何在 Jest 中模拟 useHistory Hook ?

javascript - 删除 owlCarousel 中特定宽度的 stagePadding

javascript - 在 ajax 调用的结果中查找值

javascript - 捕获 html Canvas 进行处理

javascript - javascript中Window.innerheight减去数字

reactjs - 找不到模块 'react-faq-component' 的声明文件

node.js - Jest 忽略在链接模块中 react 对等依赖