Jest, React native и asyn c -хранилище: инвариантное нарушение: собственный модуль не может быть нулевым - PullRequest
0 голосов
/ 05 апреля 2020

Запустить с 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: {
...

Как можно исправить эту ошибку или есть решения для тестирования собственного приложения React, например Jest?

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

пакет. json

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

SignInScreen-тест. js

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

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

1 Ответ

0 голосов
/ 05 апреля 2020

Это более подробно о моей посылке. 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"
    ]
  },
...