Mock not working
piranna opened this issue · 6 comments
On Node.js v8.9.1, seems that require.cache is per module and mock.js script side effects are not working. I've done some traces both on mock.js and my setupTests.js file (that calls the first one) and they are different:
console.log node_modules/react-native-mock-render/mock.js:13
{ '/home/piranna/Trabajo/Quantum/flowtag_react/node_modules/react-native/Libraries/react-native/react-native-implementation.js':
{ id: '/home/piranna/Trabajo/Quantum/flowtag_react/node_modules/react-native/Libraries/react-native/react-native-implementation.js',
filename: '/home/piranna/Trabajo/Quantum/flowtag_react/node_modules/react-native/Libraries/react-native/react-native-implementation.js',
loaded: true,
exports:
{ ActivityIndicator: [Object],
ActivityIndicatorIOS: [Object],
ART: [Object],
Button: [Object],
DatePickerIOS: [Object],
DrawerLayoutAndroid: [Object],
Image: [Object],
ImageBackground: [Object],
ImageEditor: [Object],
ImageStore: [Object],
KeyboardAvoidingView: [Object],
ListView: [Object],
MapView: [Object],
Modal: [Object],
Navigator: [Object],
NavigatorIOS: [Object],
Picker: [Object],
PickerIOS: [Object],
ProgressBarAndroid: [Object],
ProgressViewIOS: [Object],
ScrollView: [Object],
SegmentedControlIOS: [Object],
SliderIOS: [Object],
SnapshotViewIOS: [Object],
Switch: [Object],
PullToRefreshViewAndroid: [Object],
RecyclerViewBackedScrollView: [Object],
RefreshControl: [Object],
StatusBar: [Object],
SwitchAndroid: [Object],
SwitchIOS: [Object],
TabBarIOS: [Object],
Text: [Object],
TextInput: [Object],
ToastAndroid: [Object],
ToolbarAndroid: [Object],
Touchable: [Object],
TouchableHighlight: [Object],
TouchableNativeFeedback: [Object],
TouchableOpacity: [Object],
TouchableWithoutFeedback: [Object],
View: [Object],
ViewPagerAndroid: [Object],
WebView: [Object],
ActionSheetIOS: [Object],
Alert: [Object],
AlertIOS: [Function: AlertIOS],
Animated: [Object],
AppRegistry: [Object],
AppState: [Object],
AppStateIOS: [Object],
AsyncStorage: [Object],
BackAndroid: [Object],
BackHandler: [Object],
CameraRoll: [Object],
Clipboard: [Object],
DatePickerAndroid: [Function: DatePickerAndroid],
Dimensions: [Object],
Easing: [Function: Easing],
ImagePickerIOS: [Object],
IntentAndroid: [Function: IntentAndroid],
InteractionManager: [Object],
Keyboard: [Object],
LayoutAnimation: [Object],
Linking: [Function: Linking],
LinkingIOS: [Function: LinkingIOS],
NetInfo: [Object],
PanResponder: [Object],
PixelRatio: [Object],
PushNotificationIOS: [Function: PushNotificationIOS],
Settings: [Object],
StatusBarIOS: [Object],
StyleSheet: [Object],
TimePickerAndroid: [Function: TimePickerAndroid],
UIManager: [Object],
VibrationIOS: [Object],
DeviceEventEmitter: [Object],
NativeAppEventEmitter: [Object],
NativeEventEmitter: [Function: NativeEventEmitter],
NativeModules: [Object],
Platform: [Object],
processColor: [Function: processColor],
requireNativeComponent: [Function: requireNativeComponent],
ColorPropType: [Function: ColorPropType],
EdgeInsetsPropType: [Object],
PointPropType: [Object],
NavigationExperimental: [Object],
ViewPropTypes: [Object],
Children: [Object],
Component: [Function: Component],
PureComponent: [Function: PureComponent],
unstable_AsyncComponent: [Function: AsyncComponent],
createElement: [Function: createElementWithValidation],
cloneElement: [Function: cloneElementWithValidation],
createFactory: [Function: createFactoryWithValidation],
isValidElement: [Function: isValidElement],
version: '16.1.1',
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: [Object],
addons: [Object] } } } console.log src/setupTests.js:25
{}Am I doing something terrible wrong, or effectively require.cache now is per-module?
I have tested it with Node.js 9.2.0 and it's still happening.
After doing some debugging, I think problem is due to jest-runtime implementing its own require() function that has its own cache object. Seems it's done in purposse to isolate the modules and prevent side-effects... that's in fact what's doing the mock. Is there any alternative to the mock overwritting the cache entry for react-native?
jest-runtime developers have give me an advice about they don't support require.cache and using instead something like jest.mock('module-name', () => { return moduleContents }, {virtual: true}). I'll try to give it a try, but if you can check too about it and how to integrate it on react-native-mock-render someway it would be great :-D
The proposed advice works. Probably should it be added to the readme?
Thanks for sharing this issue @piranna. Improving mock.js to work in with more setups is something that we would like to do. If you'd like to open a pull request to make these changes and/or the changes to the Read Me that you suggested, I'd be happy to work with you to get them merged.
I appreciate your enthusiasm toward contributing.
Note done :-) Thank you for your compliments, it's the first time I'm working with ReactNative and I'm doing some intensive tests on it the way I see it fit us better (I come from a Node.js backend background and for cleanness I try to do testing against only the public APIs), and this module is the only one that has offered us an almost real environment to do the tests on the full application once at all :-) It has some flaws too, but I like APIs development, so it's funny to complete the missing parts :-D