首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >令牌无效或意外,正在测试纯react本机应用程序

令牌无效或意外,正在测试纯react本机应用程序
EN

Stack Overflow用户
提问于 2020-08-11 17:31:37
回答 1查看 686关注 0票数 1

我正在使用bare react原生测试驱动开发,并使用包jest和酶。我为UI组件安装了NativeBase。当我使用来自NativeBase的图标时,我得到了错误

代码语言:javascript
复制
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文件中有以下设置:

代码语言:javascript
复制
"jest": {
  "preset": "react-native",
  "testEnvironment": "jsdom",
  "setupFiles": [
    "./setup.js",
    "./node_modules/react-native-gesture-handler/jestSetup.js"
  ],
  "transformIgnorePatterns": [
    "/!node_modules\\/@expo"
  ]
},
EN

回答 1

Stack Overflow用户

发布于 2020-08-18 23:07:15

我遇到了同样的错误,并通过将此行添加到我的jest配置中修复了它。

代码语言:javascript
复制
"transform": {
  "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/fileTransformer.js"
}

并在我的项目根目录下创建文件fileTransformer.js

代码语言:javascript
复制
// 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中所述。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63355440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档