如何解决“语法错误:无法在jest中的模块”“外部使用import语句

tzcvj98z  于 2023-02-01  发布在  Jest
关注(0)|答案(1)|浏览(188)

我有一个react-native应用程序当尝试运行yarn jest时,出现以下错误:
https://i.stack.imgur.com/YOAtl.png
React Native版本:"0.64.4"
依赖关系:

"devDependencies": {
      "@babel/core": "7.12.9",
      "@babel/preset-typescript": "^7.18.6",
      "@babel/runtime": "7.12.5",
      "@react-native-community/eslint-config": "2.0.0",
      "@types/apollo-upload-client": "14.1.0",
      "@types/jest": "^29.4.0",
      "@types/react-native": "0.63.2",
      "@types/react-native-snap-carousel": "3.8.2",
      "@types/react-native-video": "5.0.1",
      "@types/react-test-renderer": "16.9.2",
      "@types/yup": "0.29.9",
      "babel-jest": "26.6.3",
      "babel-plugin-module-resolver": "4.0.0",
      "eslint": "7.14.0",
      "jest": "29.2.2",
      "metro-react-native-babel-preset": "0.64.0",
      "react-test-renderer": "17.0.1",
      "ts-jest": "^29.0.5",
      "ts-node": "10.9.1",
      "typescript": "3.8.3"
   },

测试文件如下所示:

`import 'react-native';
import React from 'react';
import App from '../src/App';

import renderer from 'react-test-renderer';
it('renders correctly', () => {
  renderer.create(<App />);
});`

我试过许多解决办法,但没有一个对我有效。

flvlnr44

flvlnr441#

这种错误在使用ES6没有配置babel的时候经常出现。
你能试着和巴别一起做测试吗?
另外,在您的jest配置中,是否将testEnvironment设置为“node”,如果是,请将其删除。

相关问题