我正在使用bare react原生测试驱动开发,并使用包jest和酶。我为UI组件安装了NativeBase。当我使用来自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"
]
},发布于 2020-08-18 23:07:15
我遇到了同样的错误,并通过将此行添加到我的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中所述。
https://stackoverflow.com/questions/63355440
复制相似问题