【问题标题】:Jest, React native and async-storage : Invariant Violation: Native module cannot be null开玩笑,反应本机和异步存储:不变违规:本机模块不能为空
【发布时间】:2020-04-04 22:39:56
【问题描述】:

在 Android 上运行

所有包都已成功链接。

我试过了

$ ./gradlew clean #in android folder

$ react-native start

$ react-native run-android

然后开玩笑显示错误

不变违规:本机模块不能为空

$ jest
 FAIL  __tests__/SignInScreen-test.js
  ● Test suite failed to run

    Invariant Violation: Native module cannot be null.

      at invariant (node_modules/invariant/invariant.js:40:15)
      at new NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:37:7)
      at Object.<anonymous> (node_modules/react-native-reanimated/src/ReanimatedEventEmitter.js:4:16)
      at Object.<anonymous> (node_modules/react-native-reanimated/src/core/AnimatedCall.js:2:1)

  console.group node_modules/redux-logger/dist/redux-logger.js:1
       action persist/PERSIST @ 05:17:04.459

  console.log node_modules/redux-logger/dist/redux-logger.js:1
       prev state {
      authReducer: {
...

我该如何解决这个错误或者有解决方案来测试像 Jest 这样的 React 原生应用程序?

jestSetupFile.js

import mockAsyncStorage from '@react-native-community/async-storage/jest/async-storage-mock';

jest.mock('@react-native-community/async-storage', () => mockAsyncStorage);

jest.mock('react-native-keychain', () => ({
  SECURITY_LEVEL_ANY: 'MOCK_SECURITY_LEVEL_ANY',
  SECURITY_LEVEL_SECURE_SOFTWARE: 'MOCK_SECURITY_LEVEL_SECURE_SOFTWARE',
  SECURITY_LEVEL_SECURE_HARDWARE: 'MOCK_SECURITY_LEVEL_SECURE_HARDWARE',
  setGenericPassword: jest.fn().mockResolvedValue(),
  getGenericPassword: jest.fn().mockResolvedValue(),
  resetGenericPassword: jest.fn().mockResolvedValue(),
}));

package.json

  "jest": {
    "preset": "react-native",
    "setupFiles": [
      "./jestSetupFile.js"
    ]
  },

SignInScreen-test.js

import {SignInScreen} from '../screens/SignInScreen';

describe('Test Component using hooks', () => {
  test('SignInScreen', () => {
    expect(<SignInScreen />).toMatchSnapshot();
  });
});

【问题讨论】:

    标签: javascript react-native unit-testing jestjs react-native-android


    【解决方案1】:

    这是关于我的 package.json 的更多详细信息

    "dependencies": {
        "@react-native-community/async-storage": "^1.7.1",
        "@react-native-community/masked-view": "^0.1.6",
        "@react-navigation/bottom-tabs": "^5.0.0-alpha.34",
        "@react-navigation/native": "^5.0.0-alpha.24",
        "@tradle/react-native-http": "^2.0.0",
        "buffer": "^4.9.1",
        "crypto": "^1.0.1",
        "events": "^1.0.0",
        "fast-deep-equal": "^3.1.1",
        "http": "^0.0.0",
        "https": "^1.0.0",
        "https-browserify": "~0.0.0",
        "perf_hooks": "0.0.1",
        "performance": "^1.2.0",
        "react": "16.11.0",
        "react-native": "0.62.1",
        "react-native-camera": "^3.15.1",
        "react-native-charts-wrapper": "^0.5.7",
        "react-native-crypto": "^2.1.0",
        "react-native-elements": "^1.2.7",
        "react-native-gesture-handler": "^1.6.0",
        "react-native-keychain": "^4.0.5",
        "react-native-paper": "^3.4.0",
        "react-native-permissions": "^2.0.9",
        "react-native-qrcode-scanner": "^1.3.1",
        "react-native-qrcode-svg": "^6.0.3",
        "react-native-randombytes": "^3.0.0",
        "react-native-reanimated": "^1.7.0",
        "react-native-safe-area-context": "^0.7.3",
        "react-native-screens": "^2.1.0",
        "react-native-svg": "^11.0.1",
        "react-native-swiper": "^1.6.0-nightly.5",
        "react-native-vector-icons": "^6.6.0",
        "react-navigation": "^4.0.10",
        "react-navigation-material-bottom-tabs": "^2.1.5",
        "react-navigation-stack": "^2.0.13",
        "react-redux": "^7.1.1",
        "readable-stream": "1.0.33",
        "redux": "^4.0.4",
        "redux-persist": "^6.0.0",
        "request-promise": "^4.2.5",
        "rn-nodeify": "^10.2.0",
        "stellar-sdk": "^3.3.0",
        "stream": "^0.0.2",
        "stream-browserify": "^1.0.0",
        "url": "~0.10.1",
        "util": "~0.10.3",
        "vm": "^0.1.0",
        "vm-browserify": "0.0.4"
      },
      "devDependencies": {
        "@babel/core": "7.6.4",
        "@babel/runtime": "7.6.3",
        "@react-native-community/eslint-config": "0.0.3",
        "babel-jest": "^25.2.6",
        "eslint": "6.6.0",
        "jest": "24.9.0",
        "metro-react-native-babel-preset": "0.51.1",
        "react-test-renderer": "16.9.0",
        "redux-logger": "^3.0.6"
      },
      "jest": {
        "preset": "react-native",
        "setupFiles": [
          "./jestSetupFile.js"
        ]
      },
    

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题并找到了解决方案here。此外,我必须在 package.json 中包含对“transformIgnorePatterns”节点的额外依赖

      【讨论】:

        猜你喜欢
        • 2020-12-13
        • 2019-05-24
        • 1970-01-01
        • 2019-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多