RealOrangeOne / react-native-mock

A fully mocked and test-friendly version of react native (maintainers wanted)
MIT License
570 stars 153 forks source link

Invariant Violation: Native module cannot be null. #137

Open miguelc1221 opened 7 years ago

miguelc1221 commented 7 years ago

Hey, not sure if this is the best place to ask but i just started using react-native-mock and I keep getting this error which I am not sure how to fix and might be related to the library.



    Invariant Violation: Native module cannot be null.

      at invariant (node_modules/fbjs/lib/invariant.js:44:7)
      at RCTNetworking.NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:32:1)
      at new RCTNetworking (node_modules/react-native/Libraries/Network/RCTNetworking.ios.js:23:165)
      at Object.<anonymous> (node_modules/react-native/Libraries/Network/RCTNetworking.ios.js:59:16)
      at Object.<anonymous> (node_modules/react-native/Libraries/Network/XMLHttpRequest.js:15:19)```
RealOrangeOne commented 7 years ago

Looking at that stack trace, it probably is a react-native-mock bug. However it's very difficult to tell what the issue is with just that stack trace. Can you try and reproduce it more clearly with an example, or provide a stack trace

miguelc1221 commented 7 years ago

I'll try to reproduce in the meantime here is some more info. The above is what I keep getting when I try to run jest, so currently can run any tests.

What led me to use react-native-mock was the fact that I couldn't mock NativeEventEmitter which we are using.

dependencies

  "dependencies": {
    "babel-plugin-transform-decorators-legacy": "1.3.4",
    "bugsnag-react-native": "2.2.0",
    "class-autobind": "^0.1.4",
    "color": "0.11.3",
    "lodash": "4.15.0",
    "moment": "2.17.1",
    "react": "15.4.2",
    "react-native": "0.42.3",
    "react-native-calendar": "0.10.0",
    "react-native-extended-stylesheet": "0.3.0",
    "react-native-fs": "2.1.0-rc.1",
    "react-native-keyboard-aware-scroll-view": "0.2.3",
    "react-native-material-kit": "0.4.1",
    "react-native-sensitive-info": "3.0.2",
    "react-native-snap-carousel": "1.2.0",
    "react-native-svg": "5.1.5",
    "react-native-vector-icons": "2.1.0",
    "react-redux": "4.4.5",
    "redux": "3.6.0",
    "redux-async-action": "0.0.4",
    "redux-thunk": "2.1.0",
    "victory-native": "0.7.0"
  },
  "devDependencies": {
    "babel-eslint": "6.1.2",
    "babel-jest": "20.0.3",
    "enzyme": "2.8.2",
    "enzyme-to-json": "1.5.1",
    "eslint": "3.7.1",
    "eslint-config-airbnb": "10.0.1",
    "eslint-plugin-import": "1.16.0",
    "eslint-plugin-jsx-a11y": "2.2.3",
    "eslint-plugin-react": "6.4.1",
    "gradle-to-js": "0.2.5",
    "husky": "0.11.9",
    "jest": "20.0.4",
    "localenv": "0.2.2",
    "react-addons-test-utils": "15.4.2",
    "react-dom": "15.4.4",
    "react-test-renderer": "15.4.2",
    "redux-mock-store": "1.2.3",
    "request": "2.81.0",
    "react-native-mock": "0.3.1"
  }

Jest config

"jest": {
    "preset": "react-native",
    "verbose": true,
    "setupFiles": ["./jestsetup.js"],
    "snapshotSerializers": [
      "enzyme-to-json/serializer"
    ],
    "transformIgnorePatterns": [
      "node_modules/(?!react-native|bugsnag-react-native)/"
    ]
  },

jestSetup.js

require('react-native-mock/mock');

jest.mock('NativeModules', () => ({
  Environment: {
    BUGSNAG_API_KEY: 'ApiKey',
  },
}));
RealOrangeOne commented 7 years ago

Hmm, interesting. I'd never considered people wanting to use react-native-mock with jest, but i guess it should work. But yes please try and narrow down exactly why it happens. It's possible it's some conflicting between react-native-mock and jest's builtin mock

skathayat commented 6 years ago

@miguelc1221 I am also experiencing the same issue. Have you found a solution to this?

doniyor2109 commented 6 years ago

+1

keithmattix commented 5 years ago

I get this because KeyboardManager is not included in the mocked native modules