开玩笑,React native和async-storage:永久违反:Native模块不能为null

时间:2020-04-04 22:39:56

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

在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();
  });
});

2 个答案:

答案 0 :(得分:0)

这是有关我的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"
    ]
  },

答案 1 :(得分:0)

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