React Native/Expo又一个SyntaxError:测试时意外的令牌导入
问题描述:
我正在开发一个使用React-Native和Expo的移动应用程序。React Native/Expo又一个SyntaxError:测试时意外的令牌导入
一切都很好,应用程序按预期运行。
但是我的测试在导入一些@expo库时没有运行。
的错误是经典之一: 语法错误:意外的令牌进口
给予埃罗的完整的上下文:
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import glyphMap
^^^^^^
SyntaxError: Unexpected token import
谷歌搜索一个解决方案,我结束了大部分时间的东西类似于提到的here。
所以我对我的的package.json写道
"jest": {
"transformIgnorePatterns": [
"/!node_modules\\/@expo"
],
"preset": "react-native"
}
我也试图改变模块名称:!
- /node_modules \ /博览会
- /node_modules \/expo/vector-icons
- /node_modules/@ expo/vector-icons
我也跟着另一个答案,并添加.babelrc与内容:
{
"presets": ["babel-preset-expo", "react-native"],
"env": {
"test": {
"plugins": [
"babel-plugin-dynamic-import-node"
]
}
}
}
不幸的是,这些解决方案都为我工作。
为了给出更多的上下文,它是一个带有世博会的React Native应用程序,运行在OSX上 除了上面的错误之外,我还收到另外5个警告。 的警告是关于所复制的模块名称(可能是不相关的,但无论如何):
jest-haste-map: duplicate manual mock found:
Module name: ErrorUtils
Duplicate Mock path: /<project_dir>/node_modules/react-native/Libraries/Core/__mocks__/ErrorUtils.js
This warning is caused by two manual mock files with the same file name.
Jest will use the mock file found in:
/<project_dir>/node_modules/react-native/Libraries/Core/__mocks__/ErrorUtils.js
Please delete one of the following two files:
/<project_dir>/node_modules/react-native/node_modules/fbjs/lib/__mocks__/ErrorUtils.js
/project_dir/node_modules/react-native/Libraries/Core/__mocks__/ErrorUtils.js
jest-haste-map: @providesModule naming collision:
Duplicate module name: camelcase
Paths: /<project_dir>/node_modules/react-native/node_modules/yargs-parser/node_modules/camelcase/package.json collides with /<project_dir>/node_modules/react-native/node_modules/yargs/node_modules/camelcase/package.json
This warning is caused by a @providesModule declaration with the same name across two different files.
jest-haste-map: @providesModule naming collision:
Duplicate module name: bser
Paths: /<project_dir>/node_modules/react-native/node_modules/bser/package.json collides with /<project_dir>/node_modules/react-native/node_modules/fb-watchman/node_modules/bser/package.json
This warning is caused by a @providesModule declaration with the same name across two different files.
jest-haste-map: @providesModule naming collision:
Duplicate module name: core-js
Paths: /<project_dir>/node_modules/react-native/node_modules/core-js/package.json collides with /<project_dir>/node_modules/react-native/node_modules/fbjs/node_modules/core-js/package.json
This warning is caused by a @providesModule declaration with the same name across two different files.
jest-haste-map: @providesModule naming collision:
Duplicate module name: yargs
Paths: /<project_dir>/node_modules/react-native/node_modules/yargs/package.json collides with /<project_dir>/node_modules/react-native/node_modules/uglify-js/node_modules/yargs/package.json
This warning is caused by a @providesModule declaration with the same name across two different files.
版本: 反应母语:0.46.1 @博览会/矢量图标:5.1.0 笑话:20.0.4
还有其他想法吗?
**** ****更新
更改transformIgnorePatterns到: “/ node_modules \/\ @世博” 不让它工作,但至少改变另一个错误。
Invariant Violation: Native module cannot be null.
at invariant (node_modules/react-native/node_modules/fbjs/lib/invariant.js:44:7)
at new NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:31:1)
at Object.<anonymous> (node_modules/expo/src/Location.js:6:26)
at Object.<anonymous> (node_modules/expo/src/Expo.js:8:1)
答
我有同样的问题。我最终通过删除并重新安装jest-expo来解决这个问题。确保您的jest-expo版本与您的世博会版本相匹配。
npm uninstall jest-expo npm cache clean # or npm cache verify (if Node 5+) npm install --save-dev jest-expo