gpt4 book ai didi

react-native - Jest 测试 React Native 不能在模块外使用 import 语句

转载 作者:行者123 更新时间:2023-12-04 11:13:18 31 4
gpt4 key购买 nike

在尝试使用 React Native 运行 Jest 测试时,我一直试图找出如何修复以下错误:

FAIL tests/App-test.js ● Test suite failed to run

/home/marijkebuurman/Desktop/sport-data-valley-questionnaire-app/node_modules/react-native/index.js:13
import typeof AccessibilityInfo from './Libraries/Components/AccessibilityInfo/AccessibilityInfo';
^^^^^^

SyntaxError: Cannot use import statement outside a module

at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
at Object.<anonymous> (node_modules/@react-navigation/native/lib/commonjs/useBackButton.tsx:3:1)

package.json
{
...
"dependencies": {
"@react-native-community/datetimepicker": "^2.3.2",
"@react-native-community/masked-view": "^0.1.8",
"@react-native-community/push-notification-ios": "^1.1.1",
"@react-native-firebase/app": "^6.7.1",
"@react-native-firebase/auth": "^6.7.1",
"@react-native-firebase/firestore": "^6.7.1",
"@react-navigation/bottom-tabs": "^5.2.6",
"@react-navigation/material-top-tabs": "^5.1.9",
"@react-navigation/native": "^5.1.5",
"@react-navigation/stack": "^5.2.10",
"axios": "^0.19.2",
"prop-types": "^15.7.2",
"react": "16.11.0",
"react-native": "0.62.2",
"react-native-background-fetch": "^3.0.5",
"react-native-elements": "^1.2.7",
"react-native-gesture-handler": "^1.6.1",
"react-native-image-picker": "^2.3.1",
"react-native-keychain": "^5.0.1",
"react-native-push-notification": "^3.1.9",
"react-native-reanimated": "^1.8.0",
"react-native-safe-area-context": "^0.7.3",
"react-native-screens": "^2.4.0",
"react-native-tab-view": "^2.14.0",
"react-native-vector-icons": "^6.6.0"
},
"devDependencies": {
"@babel/core": "^7.6.2",
"@babel/preset-flow": "^7.10.1",
"@babel/runtime": "^7.6.2",
"@bam.tech/react-native-make": "^2.0.0",
"@react-native-community/eslint-config": "^0.0.5",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.4.2",
"babel-plugin-module-resolver": "^4.0.0",
"eslint": "^6.5.1",
"jest": "^24.9.0",
"metro-react-native-babel-preset": "^0.56.4",
"react-native-dotenv": "^0.2.0",
"react-test-renderer": "16.9.0"
},
"jest": {
"preset": "react-native",
"setupFiles": [
"./jest.setup.js"
],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"
},
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|react-navigation)"
]
}
}

我已尝试解决 package.json 中的问题文件使用 transformIgnorePatterns尝试忽略 react-navigation ,但这没有奏效。
.babelrc
{
"presets": [
"module:metro-react-native-babel-preset",
"module:react-native-dotenv",
"@babel/preset-flow"
],
"plugins": [
[
"module-resolver",
{
"cwd": "babelrc",
"root": [
"./src"
],
"extensions": [
".js",
".ios.js",
".android.js"
],
"alias": {
"api": "./src/api",
"assets": "./src/assets",
"services": "./src/services",
"styles": "./src/styles",
"components": "./src/components",
"app": "./src"
}
}
]
]
}

我还安装并添加了 @babel/preset-flow到我的 .babelrc 中的预设文件,因为我在某处读到这可能会有所帮助,但没有成功。

我的测试
// import 'react-native';
import React from 'react';
import App from '../App';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

it('renders correctly', async () => {
renderer.create(<App />);
});

测试本身几乎是默认提供的 React Native Jest 测试。我已经评论了 React Native 的 import 语句,因为这在运行测试时给出了类似的错误。当该行被取消注释时,测试仍然给出关于 AccessibilityInfo 的错误。 import 语句,但没有提及 @react-navigation而是关于 ScriptTransformer.js .

我的猜测是这一切都与我的 babel 配置有关,但我一直无法弄清楚。

最佳答案

我对我的 .babelrc 尝试了很多更改文件,但我最终通过转换我的 .babelrc 修复了错误文件到 babel.config.js当前看起来像这样的文件:

module.exports = function(api) {
api.cache(true);
return {
presets: [
'module:metro-react-native-babel-preset',
'module:react-native-dotenv',
'@babel/preset-flow',
],
plugins: [
[
'module-resolver',
{
root: ['./src'],
extensions: ['.js', '.ios.js', '.android.js'],
alias: {
api: './src/api',
assets: './src/assets',
services: './src/services',
styles: './src/styles',
components: './src/components',
app: './src',
},
},
],
['transform-class-properties'],
],
};
};

除此之外,我还在我的 __mocks__ 中添加了很多模拟。不同包的目录,例如 react-native-firebase .我很确定我添加的模拟文件没有参与修复我的问题中的错误,但是通过添加模拟文件修复了其他类似的错误。
类似的错误是指具有以下错误消息的错误:
SyntaxError: Cannot use import statement outside a module

关于react-native - Jest 测试 React Native 不能在模块外使用 import 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62158612/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com