Root-App / react-native-mock-render

a fork of react-native-mock that renders
MIT License
85 stars 50 forks source link

Breaks jest-expo #33

Closed lropero closed 6 years ago

lropero commented 6 years ago

When using jest-expo as a preset for Jest (I'm working on a detached Expo project, jic) and then trying to use react-native-mock-render by adding this line to my Jest config file:

jest.mock('react-native', () => require('react-native-mock-render'), { virtual: true });

This error occurs when running my tests:

The Expo SDK requires Expo to run. It appears the native Expo modules are unavailable and this code is not running on Expo.

The failing check is at node_modules/expo/src/environment/validate.js:

if (!NativeModules.ExponentConstants) {
  throw new Error(ERROR_PASTED_ABOVE);
}

I believe this happens because both libraries are mocking react-native in a non-Object.assign(rn, rn1, rn2)-form.

Couldn't both libraries work together somehow? I believe this would allow things like mounting (as opposed to shallowing) with enzyme, jsdom, and react-native-mock-render on test suites.

lropero commented 6 years ago

btw this is in my packages.json:

"jest": {
  "preset": "jest-expo",
  "setupFiles": [
    "<rootDir>/jestConfig.js"
  ],
  "timers": "fake",
  "transformIgnorePatterns": []
},
cantide5ga commented 6 years ago

Wondering how relevant this is on detachment, but currently this is important to me too.

Thanks for the homework @lropero - interested if any project maintainers have any hints on your comments/suggestions?

AlexCatch commented 6 years ago

This is also an issue for me

bob-carson commented 6 years ago

I believe you could implement something custom by creating your own version of mock.js and/or react-native.js that did the mixing and matching in the way that you wanted.

Unfortunately, this is out of scope of what this package is intended to do right now. I'm going to go ahead and close this issue, but if you have any suggested code changes that would make that work easier for you or add to this project in a way that would make it support your use case I'm happy to review and merge your PR.