React-Native 0.59 / Babel / Jest失败

时间:2019-07-31 17:14:11

标签: react-native jestjs babeljs

从babel / jest问题处理,同时从0.52升级到RN 0.59.10。任何帮助,无论是具体的还是定向的,都是如此。

我们的测试通过了RN 0.52。我希望这些不仅仅是结构性的问题。

问题:调用/构造函数错误。在dep中的超级调用(例如RN的EventEmitter)或新的新调用(结果对象未定义)上,一些测试失败。

示例:

test suite failed to run
    TypeError: Cannot read property 'call' of undefined
      at new RCTDeviceEventEmitter (node_modules/react-native/Libraries/EventEmitter/RCTDeviceEventEmitter.js:53:5)
      at Object.<anonymous> (node_modules/react-native/Libraries/EventEmitter/RCTDeviceEventEmitter.js:84:18)
      at Object.require (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:15:31)

package.json中的Jest配置如下:

"jest": {
    "preset": "react-native",
    "automock": true,
    "setupFiles": ["<rootDir>/jest/setup.js"],  
    "transform": { "^.+\\.jsx?$": "babel-jest" },  
    ...

package.json中的(相关)部门是

    "@babel/core": "^7.5.0",
    "@babel/plugin-proposal-class-properties": "^7.5.0",
    "@babel/plugin-proposal-function-bind": "^7.2.0",
    "@babel/plugin-transform-destructuring": "^7.5.0",
    "@babel/plugin-transform-flow-strip-types": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "@babel/preset-flow": "^7.0.0",
    "@babel/preset-react": "^7.0.0",
    "@babel/runtime": "7.5.0",

    "jest": "24.8.0",
    "metro-react-native-babel-preset": "0.55.0",

我们的babel.config.json使用

'module:metro-react-native-babel-preset'

作为预设。

2 个答案:

答案 0 :(得分:0)

更新到0.59时,我遇到了同样的问题。我注意到使用jest.enableAutomock();的测试失败。我刚刚删除了该行,并更新了我的模拟设置,并再次通过了测试。我希望这会有所帮助。

答案 1 :(得分:0)

也许您可以在babel.config.js

中添加以下行
plugins: ['@babel/plugin-transform-flow-strip-types']