用Jest测试react-native时,Jest遇到意外令牌

时间:2020-05-05 02:23:57

标签: reactjs react-native testing jestjs

我想在我的react-native项目上使用笑话。我是开玩笑和本机反应的新手。 当前低于错误

Details:

/Users/sachigrannan/SKO/Alpha4-1/node_modules/react-native-iphone-x-helper/index.js:1
import { Dimensions, Platform, StatusBar } from 'react-native';
       ^

SyntaxError: Unexpected token {

  at Runtime._execModule (node_modules/jest-runtime/build/index.js:1157:58)
  at Object.<anonymous> (node_modules/react-navigation-stack/lib/commonjs/vendor/TransitionConfigs/CardStyleInterpolators.tsx:2:1)

我已经在package.json中设置了它。请帮忙!

"jest": {
    "verbose": true,
    "preset": "react-native",
    "cacheDirectory": "./cache",
    "coveragePathIgnorePatterns": [
      "./app/utils/vendor"
    ],
    "coverageThreshold": {
      "global": {
        "statements": 80
      }
    },
    "transformIgnorePatterns": [
      "node_modules/(?!(react-native|my-project|react-native-button|nodejs-mobile-react-native|react-navigation)/)"
    ]
  }

1 个答案:

答案 0 :(得分:0)

鉴于您的 transformIgnorePatterns 在引入 lib 之前已经工作,诀窍就是将“react-native-iphone-x-helper”添加到“transformIgnorePatterns”属性中。

"node_modules/(?!(react-native|my-project|react-native-button|nodejs-mobile-react-native|react-navigation|react-native-iphone-x-helper)/)"

也就是说,您最好删除“transformIgnorePatterns”并让 "preset": "react-native" 配置完成其工作。

我对预设的唯一问题是当我在嵌套的 node_module 目录中有一个未转译的依赖项时,例如。

/path/to/myproject/node_modules/@react-navigation/bottom-tabs/node_modules/react-native-iphone-x-helper/index.js:1
    import { Dimensions, Platform, StatusBar } from 'react-native';
           ^

在这种情况下,问题是另一个模块使用了不同版本的 react-native-iphone-x-helper,因此我必须更新该依赖项,以便它们对齐。如果这不可行,您可以使用 yarn 的 resolutions field