React Native / Expo又一个SyntaxError:测试

时间:2017-08-22 17:27:09

标签: react-native ecmascript-6 babel expo

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

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

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

错误是经典错误:      SyntaxError:意外的令牌导入

提供错误的完整背景:

({"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"
  }

我也尝试将模块名称更改为:

  1. /!node_modules \ / expo
  2. /!node_modules \ / expo / vector-icons
  3. / node_modules / @展/载体图标
  4. 我也跟着另一个回答并添加了.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.
    

    版本: 反应原生:0.46.1 @ expo / vector-icons:5.1.0 开玩笑: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 个答案:

答案 0 :(得分: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