我正在開發一個使用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)