react-native-mmkv icon indicating copy to clipboard operation
react-native-mmkv copied to clipboard

detox throws error when metro is run with mocks

Open Mister-CK opened this issue 3 months ago • 2 comments

When running detox with metro in mock mode. It should mock the createMMKV call. The check for this is based on a platform checker that checks isJest(). I don't think this is a good way to do this or the way metro has intended. image

The way metro works is that you configure it to prefer mock files over regular files when run in mock mode, through the sourceExts in your metro config. So it will completely ignore the regular file and use the .mock file instead. This means in the current setup that the regular createMMKV() function will not exist. This is not a good practice. What you should do instead is import only from './createMMKV'. and give the mock version of the call in your .mock file the same name. This way the call will always exist and the mock variant is picked based on how metro is started, not based on if you are running with jest (that call didn't work for me). Refactoring this might break some existing logic though. So what I did is I created a patch with PlatformChecker.mock.js which contained isJest() => true; this works because when metro is started in mock mode it will pick the isJest call from the mock file and always return true. Which I think is a better check than actually checking for jest. since the calls are dependant on mock files, not jest. I don't think this breaks the existing logic.

Mister-CK avatar Mar 11 '24 12:03 Mister-CK

Hey - thanks for your suggestions, could you submit a PR to fix this? We can test if this breaks existing behaviour or not.

mrousavy avatar Mar 11 '24 12:03 mrousavy

Thanks for the quick reply, I will see if I can make a PR with the changes tonight

Mister-CK avatar Mar 11 '24 13:03 Mister-CK