首页 文章

React Native / Expo又一个SyntaxError:测试时意外的令牌导入

提问于
浏览
1

我正在使用React-Native和Expo开发移动应用程序 .

一切都很好,应用程序按预期运行 .

但是在导入一些@expo库时我的测试没有运行 .

错误是经典错误: SyntaxError: Unexpected token import

给出错误的完整背景:

({"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 \ / expo

  • /!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"
      ]
    }
  }
}

不幸的是,这些解决方案都不适合我 .

为了给出更多的上下文,它是一个带有Expo的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.

Versions: react-native: 0.46.1 @expo/vector-icons: 5.1.0 jest: 20.0.4

还有其他想法吗?

更新

transformIgnorePatterns 更改为:"/!node_modules/@expo"不会使其正常工作,但至少会更改为另一个错误 .

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)

1 回答

  • 2

    我遇到过同样的问题 . 我最终通过删除并重新安装jest-expo解决了这个问题 . 确保您的jest-expo版本与您的Expo版本相匹配 .

    npm uninstall jest-expo npm cache clean # or npm cache verify (if Node 5+) npm install --save-dev jest-expo

相关问题