无法读取 null 错误 React-Native Jest 测试的属性“绑定”

时间:2021-04-20 12:03:42

标签: react-native jestjs babeljs

我正在使用 Jest 对我的 react-native 项目进行单元测试,但在运行 npm test 后,出现此错误:

测试套件运行失败

TypeError: /Users/walidramadan/lemonade-fashion/lemonade_mobile_client/src/screens/CartScreen.js: Cannot read property 'bindings' of null

这是我的配置:

babel.config.js:

```module.exports = {
  presets: ["@babel/preset-env", 'react-native'],
  plugins: [
    [
      'module-resolver',
      {
        extensions: [
          '.js',
          '.jsx',
          '.ts',
          '.tsx',
          '.android.js',
          '.android.tsx',
          '.ios.js',
          '.ios.tsx',
        ],
        root: ['.'],
      },
    ],
    [
      'module:react-native-dotenv',
      {
        moduleName: '@env',
        path: '.env'
      }
    ]
  ]
};```

package.json:

```{
  "name": "lemonade_mobile_client",
  "jest": {
    "verbose": true,
    "preset": "react-native",
    "transformIgnorePatterns": [
      "node_modules/(?!(jest-)?react-native|react-navigation)"
    ],
    "setupFilesAfterEnv": [
      "<rootDir>/node_modules/riteway-jest/src/riteway-jest.js"
    ]
  },
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest --config=jest.config.js",
    "lint": "eslint ."
  },
  "dependencies": {
    "@babel/core": "^7.13.16",
    "@haskkor/react-native-recaptchav3": "^1.2.1",
    "@invertase/react-native-apple-authentication": "^2.1.1",
    "@lemonadefashion/imagekit": "^1.0.0",
    "@lemonadefashion/recaptcha-actions": "^1.0.0",
    "@react-native-community/checkbox": "^0.5.7",
    "@react-native-community/masked-view": "^0.1.10",
    "@react-native-community/picker": "^1.8.1",
    "@react-navigation/bottom-tabs": "^5.11.9",
    "@react-navigation/drawer": "^5.12.5",
    "@react-navigation/native": "^5.9.4",
    "@react-navigation/stack": "^5.14.4",
    "axios": "^0.21.1",
    "babel-plugin-module-resolver": "^4.1.0",
    "babel-preset-react-native": "^4.0.1",
    "native-base": "^2.15.2",
    "react": "17.0.1",
    "react-native": "0.64.0",
    "react-native-check-box": "^2.1.7",
    "react-native-dotenv": "^2.5.5",
    "react-native-fbsdk-next": "^4.0.0",
    "react-native-gesture-handler": "^1.10.3",
    "react-native-linear-gradient": "^2.5.6",
    "react-native-pager-view": "^5.1.3",
    "react-native-picker-select": "^8.0.4",
    "react-native-reanimated": "^2.1.0",
    "react-native-safe-area-context": "^3.2.0",
    "react-native-screens": "^3.1.0",
    "react-native-tab-view": "^3.0.1",
    "react-native-vector-icons": "^8.1.0",
    "react-native-webview": "^11.3.2"
  },
  "devDependencies": {
    "@babel/preset-env": "^7.13.15",
    "@babel/runtime": "7.13.10",
    "@react-native-community/eslint-config": "2.0.0",
    "@testing-library/react-native": "^7.2.0",
    "babel-jest": "26.6.3",
    "eslint": "7.14.0",
    "jest": "^26.6.3",
    "metro-react-native-babel-preset": "^0.64.0",
    "react-test-renderer": "^17.0.1"
  },
  "transform": {
    "^.+\\.[t|j]sx?$": "babel-jest"
  }
}
```

jest.config.js:

```const { defaults } = require('jest-config');
module.exports = {
    testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|js?|tsx?|ts?)$",
    transform: {
        "^.+\\.jsx?$": "babel-jest",
        "^.+\\.mjs$": "babel-jest",
    },
    testPathIgnorePatterns: ["<rootDir>/build/", "<rootDir>/node_modules/"],
    moduleFileExtensions: ["js", "jsx", "mjs"]
}```

知道如何解决这个错误吗?

我面临另一个问题:

语法错误:不能在模块外使用导入语句

  3 |  */
  4 |
> 5 | import 'react-native';
    | ^
  6 | import React from 'react';
  7 | import App from '../App';
  8 |

测试 App-test.js 时

有什么解决办法吗?

0 个答案:

没有答案