我正在裸露的 React Native 中练习测试驱动开发,并使用包 jest 和 enzyme 。我安装了 NativeBase for UI 组件。当我使用 NativeBase 的图标时出现错误
FAIL src/screens/Welcome/Welcome.test.js
● Test suite failed to run
/Users/../node_modules/@expo/vector-icons/build/vendor/react-native-vector-icons/Fonts/AntDesign.ttf:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){
SyntaxError: Invalid or unexpected token
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1258:14)
at Object.<anonymous> (node_modules/@expo/vector-icons/src/AntDesign.ts:2:1)
我的 package.json 文件中有以下设置:
"jest": {
"preset": "react-native",
"testEnvironment": "jsdom",
"setupFiles": [
"./setup.js",
"./node_modules/react-native-gesture-handler/jestSetup.js"
],
"transformIgnorePatterns": [
"/!node_modules\\/@expo"
]
},
最佳答案
我遇到了同样的错误,并通过将此行添加到我的 Jest 配置中来修复它。
"transform": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/fileTransformer.js"
}
并在项目的根目录下创建文件 fileTransformer.js
。
// fileTransformer.js
const path = require('path');
module.exports = {
process(src, filename, config, options) {
return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';';
},
};
如 Jest 文档中所述 https://jestjs.io/docs/en/webpack#mocking-css-modules
关于react-native - 无效或意外的 token ,测试裸 react native 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63355440/