使用Jest做单元测试运行失败,TypeError: Cannot read property 'registerComponent' of undefined



  • 最近两天尝试搞自动化测试,先从最基础的单元测试搞起,读了Jes的文档和API,感觉很有可行性,于是先运行了npm test,居然失败了,异常如下:

     FAIL  __tests__/index.ios.js
      ● Test suite failed to run
    
        TypeError: Cannot read property 'registerComponent' of undefined
          
          at Object.<anonymous> (index.ios.js:23:25)
          at Object.<anonymous> (__tests__/index.ios.js:3:15)
    
    Test Suites: 2 failed, 2 total
    Tests:       0 total
    Snapshots:   0 total
    Time:        1.506s
    Ran all test suites.
    

    尝试新建了一个Demo工程,运行npm test,一切OK!搞了一个天多,查遍了github、stackoverflow,没什么头绪。如有什么见解,请不吝指教。

    package.json文件:

      "scripts": {
        "start": "node node_modules/react-native/local-cli/cli.js start",
        "test": "jest",
        "build": "babel src/ -d lib/",
        "prepublish": "npm run build",
        "flow": "flow"
      },
      "dependencies": {
        "@remobile/react-native-marquee": "^1.0.1",
        "flow-bin": "^0.46.0",
        "react": "^16.0.0-alpha.12",
        "react-native": "^0.45.1",
        "react-native-camera": "git+https://github.com/lwansbrough/react-native-camera.git",
        "react-native-easy-toast": "^1.0.6",
        "react-native-fetch-blob": "^0.10.5",
        "react-native-image-picker": "^0.26.3",
        "react-native-img-cache": "^1.4.0",
        "react-native-popup-dialog": "^0.7.29",
        "react-native-scrollable-tab-view": "^0.6.6",
        "react-native-storage": "^0.2.2",
        "react-navigation": "^1.0.0-beta.9",
        "realm": "^1.8.2",
        "rn-wheel-picker-china-region": "^1.0.3"
      },
      "devDependencies": {
        "babel-cli": "^6.24.1",
        "babel-jest": "^20.0.3",
        "babel-preset-flow": "^6.23.0",
        "babel-preset-react-native": "2.1.0",
        "flow-bin": "^0.46.0",
        "jest": "20.0.4",
        "react-test-renderer": "16.0.0-alpha.12",
        "regenerator-runtime": "^0.10.5"
      },
      "jest": {
        "preset": "react-native",
        "automock": true
      }
    

    .babelrc文件:

    {
      "presets": ["react-native", "flow"]
    }
    


  • @晴明 请问咱们有谁用Jest搞过单元测试吗?


登录后回复