react-native - 无效或意外的 token ,测试裸 react native 应用程序

标签 react-native testing jestjs enzyme

我正在裸露的 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/

相关文章:

reactjs - react 路由器,history.push 之后的代码运行?

php - 通过单元测试验证配置文件?

c# - 使用哪种测试方法?

javascript - 开 Jest 模拟 aws-sdk ReferenceError : Cannot access before initialization

react native PanResponder : OnMoveShouldSetPanResponder Not Working

objective-c - 体系结构 x86_64 的 undefined symbol : "std::terminate()", 引用自

react-native - 嵌套平面列表不变违规 : A VirtualizedList contains a cell which itself contains more than one VirtualizedList

ruby-on-rails - 被 RSpec 惊呆了

jestjs - 什么是过时的快照和快照文件?

javascript - 在 React Jest/Enzyme 中测试子组件的条件渲染